Muhammad Mussa
Muhammad Mussa

Reputation: 5

Trying to change a variable in use to what the user inputs

I have not found an answer that satisfies my needs or is simple enough for me to actually understand as I'm relatively new to python!

I have a variable labelled difficulty which asks the user to input a string to state whether they want easy medium or hard mode. Unfortunately I am unable to successfully have python check the input for the words used and give them what i want, i end up with "easy is not defined" or "medium is not defined" or "hard is not defined." Is there a way for me to get round this problem? Here is a small section of my code which the problem is enclosed to:

difficulty=input("What difficulty do you wish to choose, easy,medium or hard?")
   if difficulty==easy:
        print("You have chosen the easy mode, your test will now begin")
        print("")

    elif difficulty==medium:
        print("You have chosen the medium mode, your test will now begin")

    else:
        print("You have chosen the hard mode or tried to be funny, your test 
        will now begin")

Upvotes: 0

Views: 49

Answers (2)

PeterH
PeterH

Reputation: 868

You are trying to get a string from the user (input will return a string) then compare it to another string in this case 'easy' or 'medium'. Here is a link to a google dev article talking about some of the basic things you can do to strings in python.

difficulty = input("What difficulty do you wish to choose, easy,medium or hard?")
if difficulty == 'easy':
    print("You have chosen the easy mode, your test will now begin")
    print("")

elif difficulty == 'medium':
    print("You have chosen the medium mode, your test will now begin")

else:
    print("You have chosen the hard mode or tried to be funny, your test will now begin")

When you put easy or medium in your code you are telling python that they are variables (link to python variable tutorial) not strings. In this case you have not defined the easy variable yet ie: easy = 'some data'. Because you have not defined it python does not know what to do with easy it will throw an error.

Upvotes: 1

steliosbl
steliosbl

Reputation: 8921

First of all, fix your indentation (if it is not a problem only in your example). Second, you need to put easy, medium, and hard in either single ' or double " quotes:

difficulty=input("What difficulty do you wish to choose, easy,medium or hard?")
if difficulty=="easy":
    print("You have chosen the easy mode, your test will now begin")
    print("")

elif difficulty=="medium":
    print("You have chosen the medium mode, your test will now begin")

else:
    print("You have chosen the hard mode or tried to be funny, your test 
    will now begin")

If you don't put them in quotes, then you aren't comparing difficulty to the word easy, but rather to the variable called easy. This of course causes an error, since no such variable exists. The quotes however tell python to interpret easy as a string.

Upvotes: 0

Related Questions