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

您如何比较2个变量与不同类型的数据?

您如何比较2个变量与不同类型的数据?

慕工程0101907 2021-04-08 04:21:34
比较两个名为target的变量,并使用Python 3猜测一个是整数,一个是字符串。import randomimport systarget = random.randint(1, 20)name = input ('Hello, what is your name?')print ('Hello, %s. I am thinking of a number from 1 to 20. You will have 3 tries, and after each try, I will tell you if the number that I am thinking of is lower or higher. Try to guess it!' % name) guess = input ('What number do you think I am thinking of?')if guess == target:    print ('Congratulations! You won! Please play again!')    sys.exit()else:    print ('You did not guess the number correctly.')    if target < guess:        print ('The number that I am thinking of is smaller than your guess. Try again')    else:        print ('The number that I am thinking of is larger than your guess. Try again!')
查看完整描述

1 回答

?
慕后森

TA贡献1802条经验 获得超5个赞

您可以通过以下方式简单地将输入从字符串解析为整数:

guess = int(input('What number do you think I am thinking of?'))

然后,您可以将其自由地与所需的任何整数进行比较。


查看完整回答
反对 回复 2021-04-20
  • 1 回答
  • 0 关注
  • 133 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

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

帮助反馈 APP下载

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

公众号

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