我正在尝试用python创建一个简单的程序,但是我卡住了

时间:2018-09-10 22:20:00

标签: python python-3.x

我正在尝试在python中编写一个非常简单的程序,但是它在错误窗口中指出语法无效。我不明白这是怎么回事:

print('What is your name?')
YourName = input()
if YourName == "Alice":
    print('Hello Alice')
elif print('How old are you?')
    age = input()
elif age < 4:
        print('And you know how to write at young age!')

2 个答案:

答案 0 :(得分:2)

尝试一下:

print('What is your name?')
YourName = input()
if YourName == "Alice":
    print('Hello Alice')
else:
    print('How old are you?')
    age = int(input())
    if age < 4:
        print('And you know how to write at young age!')

还有一线:

print("Hello Alice") if input("What is your name ")=="Alice" else print('And you know how to write at young age!') if int(input("How old are you? "))<4 else None

答案 1 :(得分:1)

您的代码问题

欢迎光临!以下行是错误的:

elif print('How old are you?')

这背后有两个主要原因:

  1. print('How old are you?')不是正常情况。尽管从技术上讲这很好,因为编译器会将其评估为None类型,并将其视为False,但是您可能希望用概念上有意义的东西替换该部分。
  2. elif之后,您丢失了一个冒号。例如:

    elif (int(input()) < 4):
        #your code
    

解决方案

print('What is your name?')
YourName = input()
if YourName == "Alice":
    print('Hello Alice')
else:
    print('You are not Alice! How old are you?')
    if int(input()) < 4:
        print('You know how to write at young age!')