1

So I decided to make some silly simple code to practice on my own, and I have trouble finding what it is im doing wrong. Python displays 'syntax error' but im not sure what im doing wrong.

>>> x= str(input("Select a colour: Red, Blue or Green"))
Select a colour: Red, Blue or Green:
>>>if x==str(Red):
...    print("Charmander")
...elif x==str(Blue):
...    print("Squirtle")
...elif x==str(Green):
...    print("Bulbasaur")
... else:
...    exit()
3
  • String literals should be enclosed in quotes. For example, "Red" or 'Red' Commented Jun 6, 2016 at 7:57
  • And you don't need the str before them. Commented Jun 6, 2016 at 7:57
  • The string problem should raise a NameError and the else should raise an IndentError but I don't see where the SyntaxError come from. Commented Jun 6, 2016 at 8:06

1 Answer 1

5

Couple things here..

  1. This isn't necessarily wrong (the code will still work) but there's no reason to convert input to a str, it already is a string
  2. Strings need to be in quotes, not inside str(). Use "Red" instead
  3. Maybe use better variable names, x is unclear
  4. The last exit is useless

Your code fixed would look something like:

selected_color = input("Select a colour: Red, Blue or Green ")
if selected_color == "Red":
    print("Charmander")
elif selected_color == "Blue":
    print("Squirtle")
elif selected_color == "Green":
    print("Bulbasaur")

Improvements

Since you are just converting a colour to a pokemon you could use a dict here

pokemonColors = {"Red" : "Charmander", "Blue" : "Squirtle", "Green" : "Bulbasaur"}
print(pokemonColors[input("Select a colour: Red, Blue or Green ")])

Notice that this will give a KeyError for a color not in the dict, you could use .get() with a default to fix this if you want though.

Sign up to request clarification or add additional context in comments.

10 Comments

Thanks, that really helped a lot. I got the code to sort of work without using dictionaries by using the first tips you provided, since I havent covered dictionaries yet. I'm really just learning it for the first time and trying to get more familiar and comfortable with the language.
@RamisRafay Glad to help, don't worry about them if they are too confusing. I just wanted to show you that there is a better way to solve the problem, you don't have to use it until you are ready though.
@Keatinge From the way it seems to be used, I assume dictionaries map one sort of 'word' to its intended meaning?
@RamisRafay That's the way its used here, but dictionaries are much more broad than that. It looks up the value for any key in a table. It doesn't have to be strings (but it is in this case). You can learn about them at: tutorialspoint.com/python/python_dictionary.htm
@Keatinge Also if its not too much trouble, how do i get the required input to appear at the end of the code rather than in the middle of it? (I hope my wording made sense here). As soon as you write: input( "Choose a colour: Red, Blue or Green"), it makes the next line the only area where one can input the answer and I would much prefer that to appear at the end.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.