Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
243 views
in Technique[技术] by (71.8m points)

Why am I getting invalid syntax in an 'If else' statement? (Python)

I am creating my first python project, and I am fairly new to this. So I do not understand why I am getting an error message in VSCode. Here is the code:

import random
import numpy
from numlst import numlist_1

#  this is not in the actual code, but I wanted to say that the import numlist
#  1 is an actual code file in my game, and it has nothing to do with the python
#  module in the standard python library

def get_name():
    name = input("Before we start, what is your name?")
    print("You said your name was: " + name)
    
tries = 5

# game_over = 5
# while game_over < 6:
def get_number(get_name, random):
    get_name()
    print(input("guess your number, since this is the first level you need to choose a number between 1 and 10  "))
    number = random.choice(numlist_1)
    if input == number:
        print("Congratulations! You got the number correct!")
    else:
        tries -1
        print("Oops, you got the number wrong you have: " +  str(tries)  + " tries left")
        print(input(" Do you want to try again?")):
            if input == "yes":
                get_number(get_name, random)
            elif input == "no":
                print("Well goodbye then... I hope you have a great rest of your day")



# get_number(get_name, random)

get_number(get_name, random)

So I am getting this invalid syntax message whenever I run this. It is complaining about the fact I have the ':' after the

        print(input(" Do you want to try again?")):

I have tried removing this and what happens is, in the terminal the code runs perfectly until I get the message: " Do you want to try again?", so no matter if I answer anything (yes, no, blablabla), it just returns.

Also, another question is how do I make the while loop here work? I know that the while loop is commented out, but if I actually format it correctly, I get no output from the terminal when I run the code with the while loop (I am beginning to doubt that it is even a while loop at this point). But that is the next step for me since I want to solve the prior issue first.

I would appreciate if you can answer one of the questions or both of them if you have the time to. Thank you for taking the time to read this, and an even bigger thank you if you tried to solve this.

question from:https://stackoverflow.com/questions/65893615/why-am-i-getting-invalid-syntax-in-an-if-else-statement-python

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You don't need (and cannot have) a colon and an indented block after a print statement.

If you want to input a value and check if it is yes or no, you need to capture the return value from input.

answer = input("Do you want to try again?")
if answer=='yes':
   ...
elif answer=='no':
   ...

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...