2021年1月25日星期一

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.

https://stackoverflow.com/questions/65893615/why-am-i-getting-invalid-syntax-in-an-if-else-statement-python January 26, 2021 at 07:04AM

没有评论:

发表评论