为了账号安全,请及时绑定邮箱和手机立即绑定

我正在尝试用 python 编写一个简单的程序,但我卡住了

我正在尝试用 python 编写一个简单的程序,但我卡住了

茅侃侃 2021-06-11 17:01:37
我正在尝试在 中制作一个非常简单的程序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 回答

?
德玛西亚99

TA贡献1770条经验 获得超3个赞

试试这个:


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



查看完整回答
反对 回复 2021-06-15
?
蝴蝶不菲

TA贡献1810条经验 获得超4个赞

你的代码有问题

欢迎!以下行是错误的:


elif print('How old are you?')

这背后有两个主要原因:


print('How old are you?')不是正常情况。尽管这在技术上很好,因为您的编译器会将其评估为类型None并将其视为 ,但False您可能希望用概念上有意义的内容替换该部分。

你在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!')


查看完整回答
反对 回复 2021-06-15
  • 2 回答
  • 0 关注
  • 116 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号