lundi 24 juillet 2017

Semantic guess the number game errors - Python3.x

I am 11 years old and am keen programmer learning Python.

1) I am programming a guess the number game and when I ask the user if they want to play again, I get a semantic error (I think this is the correct way to describe it) where if I input "no", "n", "NO" or "N", the if statement is still executed, causing the loop() function to run again, after calculating scores. Take a look at the following image (sorry about the cluttered windows).

Play again error: http://ift.tt/2uRdmWQ

Here is a link to the rest of the program: http://ift.tt/2uu324o (I only have 8 reputation points) , but I am looking at just this snippet:

def play_again():
    again = input("\nWould you like to play again? y/n " + prompt)
    if again.upper() == "Y" or "YES":
        global gu_num
        percent = gu_num * 10
        score = 100 - percent
        highscores = [{round: (score)}]
        current_score = {round: (score)}
        highscores.append(current_score)
        print("Lets see if you can beat your score of " + str(current_score[round]) + ".\nHere we go!")
        gu_num = 0
        loop()
    elif again.upper() == "N" or "NO":
        print("Ok then.\nThank you for playing Guess The Number and I hope to see you again soon!\nThis game was created and devoloped by Kiran Patel (me), age 12. If you liked this game or want to talk to me about -->anything<--, please do email me at kiran@inteleyes.com. It'll make me happy! Thank you for playing Guess the number with me.\n\n program was developed by Kiran Patel in 2017 ad.")
        input("\n\nPress the enter key to exit")
        quit()
    else:
        print("Sorry, I don't understand. Please try again:")
        play_again()

2) I'm having a similar problem with the part of my code which starts to handle files. When I input a 'no' (same if expression) the program will execute the part of that if statement which creates the file (take a look at this image: file saving result and this image: file operations code). Note that the 'file operations code' image prints out the file-save error message because I hadn't given perms to write in prog'/files folder. I know it has tried to save the file because of the error message that was printed out (I intended the error message to be printed out).

3) Also, does anyone know what #!/usr/bin/python means? I know its hashtagged out, but I have seen it before like this and I was wondering what it means.

As always, ANY help will be appreaciated, so please don't hesitate on adding something that is not directly relevant to the question, because I may well have missed it (unless it is completely irrelevant).

Once again, thanks in advance for your help.

From Kiran

Aucun commentaire:

Enregistrer un commentaire