gpt4 book ai didi

Python变量字符串错误

转载 作者:太空宇宙 更新时间:2023-11-03 15:21:38 25 4
gpt4 key购买 nike

我将一个变量放入打印语句中,但收到此错误消息:

Traceback (most recent call last):
File "C:\Users\Samuel\workspace\First Python Project\com\fireboxtraining\Person.py", line 13, in <module>
print("Dude, %s is not yes or no... come on.") % (game)
TypeError: unsupported operand type(s) for %: 'NoneType' and 'str'

这是我的代码:

    '''
Created on Apr 17, 2017

@author: Samuel
'''
game = input("Would you like to play a game? Please answer with \"Yes\" or \"No\".")
game = game.lower()
if game == "yes":
print("Great! Let's play.")
elif game == "no":
print("Ok, I guess we won't play.")
else:
print("Dude, %s is not yes or no... come on.") %(game)

我刚刚学习Python,我不明白为什么这不起作用。请帮忙!

最佳答案

这是一个简单的括号被关闭。你有:

print("Dude, %s is not yes or no... come on.") %(game)

应该是:

print("Dude, %s is not yes or no... come on." % (game) )

关于Python变量字符串错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43485298/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com