返回的值未定义

时间:2016-08-11 04:21:10

标签: python python-3.x

我正在创建一个猜谜游戏,我已经创建了两个函数。一个用于输入用户输入,另一个用于检查用户输入是否正确。

def getGuess(maxNum):
    if maxNum == "10":
        count=0
        guess = -1
        guessnum = [ ]
        while guess >10 or guess<0:
            try:
                guess=int(input("Guess?"))
            except:
                print("Please enter valid input")
        guesses.append(guess)
        return guesses
        return guess

def checkGuess(maxNum):
    if maxNum == "10":
        if guess>num1:
            print("Too High")
        elif guess<num1:
            print ("Too Low")
        else:
            print("Correct")
            print (guesses)

,主要代码是

if choice == "1":
    count = 0
    print("You have selected Easy as the level of difficulty")
    maxNum= 10
    num1=random.randint(0,10)
    print (num1)
    guess = 11
    while guess != num1:
        getGuess("10")
        checkGuess("10")
        count = count+1
    print (guess)

虽然函数返回用户猜测代码总是将猜测值设为11.如果我没有定义猜测,那么它也不起作用。请帮忙。

2 个答案:

答案 0 :(得分:0)

首先,您要返回两个值。 return语句也可以充当break,因此不会调用第二个return。此外,您不会将返回值存储在任何位置,因此它会消失。

以下是您编辑的代码:

def getGuess(maxNum):
    if maxNum == "10":
        guess = -1
        while guess >10 or guess<0:
            try:
                guess=int(input("Guess?"))
            except:
                print("Please enter valid input")
        return guess

def checkGuess(maxNum, guess, num1):
    if maxNum == "10":
        if guess>num1:
            print("Too High")
        elif guess<num1:
            print ("Too Low")
        else:
            print("Correct")
            return True
        return False

if choice == "1":
    count = 0
    print("You have selected Easy as the level of difficulty")
    maxNum= 10
    num1=random.randint(0,10)
    print (num1)
    guess = 11
    guesses = []
    while guess != num1:
        guess = getGuess("10")
        guesses.append(guess)
        hasWon = checkGuess("10", guess, num1)
        if hasWon:
            print(guesses)
            break
        count = count+1
You have selected Easy as the level of difficulty
2
Guess?5
Too High
Guess?1
Too Low
Guess?2
Correct
[5, 1, 2]
>>> 

答案 1 :(得分:0)

你有一种我称之为“类型和希望”的编程风格。 maxNum似乎在数字和字符串之间反弹,表明您没有考虑过您的方法。下面是一个返工,每个例程尝试在没有额外变量的情况下做一些明显有用的事情。 (我已经离开了最初的choice逻辑,因为它对这个例子没有贡献,可以放入你的选择框架。)

import random

def getGuess(maxNum):
    guess = -1

    while guess < 1 or guess > maxNum:
        try:
            guess = int(input("Guess? "))
        except ValueError:
            print("Please enter valid input")

    return guess

def checkGuess(guess, number):
    if guess > number:
        print("Too High")
    elif guess < number:
        print("Too Low")
    else:
        print("Correct")
        return True

    return False

print("You have selected Easy as the level of difficulty")

maxNum = 10

maxTries = 3

number = random.randint(1, maxNum)

count = 1

guess = getGuess(maxNum)

while True:
    if checkGuess(guess, number):
        break

    count = count + 1

    if count > maxTries:
        print("Too many guesses, it was:", number)
        break

    guess = getGuess(maxNum)

要考虑的几个具体事项:避免使用except而不了解您期望的异常;避免将数字作为字符串传递 - 在输入时将数字字符串转换为数字,在输出时将数字转换为数字字符串,但在两者之间使用实际数字。