2016-07-22 40 views
1

以下代码是用于猜数字游戏。它没有错误但没有输出。我怎样才能进一步改进代码?为什么这个号码猜测程序不能通过这条线?

它一直工作到这条线print("Your Number has been Generated,Enter your guess Below"),但没有给出任何输出。

 #Guess the number 

    from random import randint 

    print("Welcome to Guess the Number Game:") 
    print("Press 'g' to Generate Number between 0 and 100:") 

    cmd=input("->") 

    if cmd=='g': 
     num=randint(0,100) 
    else: 
     print("Unknown Input") 


    def chk(x): 
     if int(x)==True: 
      chk=True 
     else: 
      chk=False 

    def diff(x,y): 
     diff=(x-y) 
     return diff 

    print("Your Number has been Generated,Enter your guess Below") 
    guess=input("->") 

    chk(guess) 

    while chk==True: 
    diff(num,guess) 
    if diff==0: 
    print("Congrats! correct Guess") 

    elif diff>10: 
    print("Oh! too high") 

    elif diff<5 and diff>0: 
    print("very close!Better Luck Next time") 

    else: 
    diff<0 
    print("Sorry!too Low") 

回答

0

你有几个问题与,其中一些可能抄写的问题:

def chk(x): 
    if int(x)==True: 
     chk=True 
    else: 
     chk=False 

我想这是为了check输入。根据python的版本,这总是将chk设置为false。这是因为int会返回一个整数,它与布尔值True的类型不同。目前还不清楚你想在这里实现什么。看起来你想检查输入是一个数字。相反,为什么不使用isdigit?因为,此刻,你的代码返回false你从来没有进入while

您还没有正确缩进while循环,作为一个经验法则,像ifwhile语句需要一个新的生产线和压痕之后。

所以这可能是更好的代码使用方法:

# keep guessing while it's a digit 
while guess.isdigit(): 
    # assign the difference to a variable 
    # don't forget to indent after the while statement 
    diff = diff(num, int(guess)) 
    if diff==0: 
     # indent again! 
     print("Congrats! correct Guess") 

    elif diff>10: 
     print("Oh! too high") 

    elif diff<5 and diff>0: 
     print("very close!Better Luck Next time") 

    elif diff<0: 
     print("Sorry!too Low") 

那你不需要chk的所有方式。

+0

你是对的,我用def chk(x)来检查输入是否是数字,我会记住缩进规则。感谢您的帮助 –

+0

我在chk(x)中想的是,如果输入是非数字的任何东西,即如果一个字符串,那么int(字符串)将不可能,因此条件int(x)== true将假,现在我明白了。感谢您的洞察 –

+0

@RohanDwivedi究竟发生了什么,会抛出异常。 – Pureferret

0

存在多个问题,我以评论已经给

from random import randint 

print("Welcome to Guess the Number Game:") 
print("Press 'g' to Generate Number between 0 and 100:") 

cmd=input("->") 

if cmd=='g': 
     num=randint(0,100) 
else: 
     print("Unknown Input") 


def chk(x): 
    if x.isnumeric(): # needs to be '.isNumeric()' as only the only integer that will return true is '1' 
      chk=True 
    else: 
      chk=False 
    return chk #Returns Boolean 

def diff(x,y): 
     return (y-x)# this needs to y-x to be correct 

print("Your Number has been Generated,Enter your guess Below") 
guess=input("->") 

while chk(guess): #or chk(guess) == True 
    difference = diff(num,int(guess))#guess needs to be an INTEGER 
    if difference==0: 
     print("Congrats! correct Guess") 
     exit() 

if difference>10: 
    print("Oh! too high") 

elif difference<5 and difference>0: 
    print("very close!Better Luck Next time") 

elif difference<0: 
    print("Sorry!too Low") 
print("Enter your guess Below")#allows you to put in another guess 
guess=input("->")#sets the guess variable 
chk(guess) 
1

很好的答案显示,但你可以用很“平”脚本做到这一点,没有定义的功能。这仅仅作为一个例子:

from random import randint 

# welcome 
print("Welcome to Guess the Number Game:") 

# loop until 'g' is given 
cmd = '' 
while not cmd == 'g': 
    print("Press 'g' and '<enter>' to Generate Number between 0 and 100:") 
    cmd = input("->")  

# generates a random number 
num = randint(0,100) 
print("Your Number has been Generated,Enter your guess Below") 

# loops until correct answer is guessed 
while True: 
    try: 
     guess=int(input("guess->")) 
    except ValueError: 
     print('Numbers only please') 
     continue 
    if guess > num: 
     print('too high') 
    if guess < num: 
     print('too low')   
    if guess == num: 
     print("Congrats! correct Guess") 
     break