我正在尝试制作一个用户猜测数字的数字猜谜游戏。理想情况下,我希望将数字的整数和字符串版本都作为有效答案。据我所知,我必须将raw_input设置为整数或保留它(作为字符串),然后创建一个if语句,如果输入与起始数据类型不同,则该语句将输入转换为整数或字符串。如果%s用于字符串,而%d用于整数,我也不知道如何使用字符串连接来格式化else语句。我为我在帖子或代码中所犯的任何新手错误道歉,我几周前才开始使用。这是我的代码中让我犯错的部分:
call = int(raw_input('Guess: ')) #defining for use outside of the function definitions
if call.isalpha():
str(call)
def call_and_response():
if call == 76 or call.lower() == 'seventy-six':
print 'Fantastic! 76 is correct. \nThank you for playing!'
else:
print 'Good try, but %s is incorrect.' %(call)
call_and_response()发布于 2020-12-03 15:37:56
使用isdigit()判断输入是否为数字。
call = raw_input('Guess: ')
if call.isdigit():
call = int(call)
correct = call == 76
else:
correct = call.lower() == 'seventy-six'
if correct:
print 'Fantastic! 76 is correct'
else:
print 'Good try'https://stackoverflow.com/questions/65121598
复制相似问题