无法找出我在第1行上遇到的语法错误python

时间:2019-02-17 03:18:59

标签: python

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))

3 个答案:

答案 0 :(得分: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()方法采用任意数量的参数,但它们分为2种特定类型:

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

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

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

答案 1 :(得分:1)

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

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

希望对您有帮助。

答案 2 :(得分:0)

print(“哇,你{}:如果您对阅读已经有信心.....”。.format(age)

上面的代码缺少“)”