无法弄清楚我在1-蟒蛇线上遇到的语法错误

问题描述 投票:-4回答:3
first_name = input("What is your first name?:")
print("Hello, {}").format(first_name)


if first_name == "Craig":
    print(first_name, "is learning Python")
elif first_name == "Maximiliane":
    print(first_name, "is learing with fellow students.   Me too")
else:
    # This is just in case we have a younger user who can't yet read
    age = int(input("How old are you?  "))
    if age <= 6:
        print("Wow you're {}: if you're confident with your reading already.....".format(age)
    print("You should totally learn Python, {}!".format(first_name))
print("Have a great day {}!".format(first_name))
python
3个回答
1
投票

必须在print函数中调用.format函数,如下所示:

print( "Hello, {}".format(first_name) )

我希望它有所帮助。


1
投票

问题出在这个声明中:

print("Hello, {}").format(first_name)

该线应该采用format()方法

print("Hello, {}".format(first_name))

此外,你已经忘记了这条线的支架!

print("Wow you're {}: if you're confident with your reading already.....".format(age)) #I have added the last bracket right now

为了帮助您再也不会遇到此语法问题,format()方法接受任意数量的参数,但它们分为两种特定类型:

位置参数 - 可以使用花括号内的参数索引访问的参数列表{index}

关键字参数 - key = value类型的参数列表,可以使用花括号内的参数键访问{key}

这将解决您的问题!祝好运!


0
投票

打印(“哇你是{}:如果你对你的阅读有信心......”。格式(年龄)

在代码上面缺少“)”

© www.soinside.com 2019 - 2024. All rights reserved.