python新手问题——int(input())出错
天蓬老师
天蓬老师 2017-04-17 15:06:36
0
2
596

新人初学python两天,在编写一个最基础的猜数字小游戏时遇到问题。

from random import randint num = randint (1,200) print ('Guess what I think.') bingo = False while bingo == False: answer = int(input()) if answer < num: print ('%d too small' % int(input())) if answer > num: print ('%d too big' % int(input())) if answer == num: print ('Bingo!\n%d is right.' % int(input())) bingo == True

主要问题在于当Run起来的时候,程序会在第一次输入数字时没有反应,第二次输入才会给出回答。我试过了%s,%d,以及+号,但却都会出现慢一拍的情况。
而在完全相同的代码没有使用%时却可以立刻得到回答,请问这是怎么回事?

天蓬老师
天蓬老师

欢迎选择我的课程,让我们一起见证您的进步~~

reply all (2)
Ty80
from random import randint num = randint (1,200) print ('Guess what I think.') bingo = False while bingo == False: answer = int(input()) if answer < num: print ('%d too small' % answer) if answer > num: print ('%d too big' % answer) if answer == num: print ('Bingo!\n%d is right.' % answer) bingo == True
    小葫芦

    Each input() will ask you to enter a number. Your three if judgments all require a new input. You should use a variable answer to save the input variable value like the one above. The next three if judgments use this Just save the value

      Latest Downloads
      More>
      Web Effects
      Website Source Code
      Website Materials
      Front End Template
      About us Disclaimer Sitemap
      php.cn:Public welfare online PHP training,Help PHP learners grow quickly!