繁体   English   中英

为什么在“If else”语句中出现无效语法? (Python)

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

我正在创建我的第一个 python 项目,我对此很陌生。 所以我不明白为什么我在 VSCode 中收到错误消息。 这是代码:

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)

所以每当我运行它时,我都会收到这个无效的语法消息。 它抱怨我在

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

我已经尝试删除它并且发生的情况是,在终端中代码运行完美,直到我收到消息:“你想再试一次吗?”,所以无论我回答什么(是,否,blablabla),它只是返回。

另外,另一个问题是如何使这里的while循环工作? 我知道while循环被注释掉了,但是如果我真的正确格式化它,当我用while循环运行代码时,我不会从终端得到output(我开始怀疑它甚至是一个while循环)。 但这对我来说是下一步,因为我想先解决前面的问题。

如果您有时间可以回答其中一个问题或两个问题,我将不胜感激。 感谢您花时间阅读本文,如果您尝试解决此问题,我们将更加感谢您。

我正在创建我的第一个 python 项目,我对此很陌生。 所以我不明白为什么我在 VSCode 中收到错误消息。 这是代码:

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)

所以每当我运行它时,我都会收到这个无效的语法消息。 它抱怨我在

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

我已经尝试删除它并且发生的情况是,在终端中代码运行完美,直到我收到消息:“你想再试一次吗?”,所以无论我回答什么(是,否,blablabla),它只是返回。

另外,另一个问题是如何使这里的while循环工作? 我知道while循环被注释掉了,但是如果我真的正确格式化它,当我用while循环运行代码时,我不会从终端得到output(我开始怀疑它甚至是一个while循环)。 但这对我来说是下一步,因为我想先解决前面的问题。

如果您有时间可以回答其中一个问题或两个问题,我将不胜感激。 感谢您花时间阅读本文,如果您尝试解决此问题,我们将更加感谢您。

我正在创建我的第一个 python 项目,我对此很陌生。 所以我不明白为什么我在 VSCode 中收到错误消息。 这是代码:

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)

所以每当我运行它时,我都会收到这个无效的语法消息。 它抱怨我在

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

我已经尝试删除它并且发生的情况是,在终端中代码运行完美,直到我收到消息:“你想再试一次吗?”,所以无论我回答什么(是,否,blablabla),它只是返回。

另外,另一个问题是如何使这里的while循环工作? 我知道while循环被注释掉了,但是如果我真的正确格式化它,当我用while循环运行代码时,我不会从终端得到output(我开始怀疑它甚至是一个while循环)。 但这对我来说是下一步,因为我想先解决前面的问题。

如果您有时间可以回答其中一个问题或两个问题,我将不胜感激。 感谢您花时间阅读本文,如果您尝试解决此问题,我们将更加感谢您。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM