Python:简单的while循环

时间:2017-03-13 07:44:04

标签: python python-2.7 while-loop

我需要生成一个介于1到9之间的随机数,两者都包含在内,然后让用户猜测数字,然后告诉他们猜测是否过低,过高或过高。

我检查了许多不同的答案,我只是无法发现错误,但显然,它在while语句之后卡在代码行中,并且不会读取“if”s。我正在使用Python 2.7.13。所以这是代码:

import random

list1 = range(1, 10)
times =  0
numcpu = random.choice(list1)
running = True
while running:
    numhum = raw_input("Try to guess the random number between 1 and 9 the CPU generated: ")
    if numhum in list1:
        if numhum == numcpu:
            print ("You got it mate! It only took you " + times + " times")
            break
        else:
            print("Try again")
            continuar = str(input("Continue (y/n)?"))
            if continuar == "y":
                times = times + 1
            elif continuar == "n":
                print "Ciao"
            else:
                print "Don't mess around with me, human"
    elif numhum == "exit":
        print("Ciao")
        running = False
    else:
        "Don't mess around with me, human"

1 个答案:

答案 0 :(得分:0)

在行

中使用raw_input代替input
continuar = str(input("Continue (y/n)?"))