Python获取用户输入错误 [英] Python getting user input errors

查看:39
本文介绍了Python获取用户输入错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的程序,提示用户输入 1-9 之间的数字,如果之前输入过该数字,它会要求用户输入另一个数字.用户有 10 次尝试或尝试.

I have a simple program that prompts user to enter number between 1-9 and if the number has been entered previously it will ask the user to enter another number. The user has 10 tries or attempts.

这只是我想做的一部分,但是我在将参数从 getNum 方法传递到调用它的方法时遇到了问题.如果我输入1-9之间的数字,就没有问题.当我输入之前输入的号码并且提示要求输入另一个号码时,问题就开始了.

This is only part of what I wanted to do but I am having problems passing the parameter from the getNum method to the method that calls it. If I enter the number between 1-9, it has no problem. The problem starts when I entered the number previously entered and the prompts ask for another number.

def getNum(numList):
    num = input("Pick your number: ")

    if num <= 0 or num >9:
        print 'Invalid number. Please try again.'
        getNum()

    if num in numList:
        print 'Number taken. Please try again.'
        getNum()

    else: 
        return num  

inputList = []
endGame = True
choice = 0  
attempts = 0 
while endGame == False or attempts < 10:

    userNum = getNum(inputList)
    print 'Number entered:', userNum
    inputList.append(userNum)
    print inputList

    attempts += 1 

谁能告诉我我在这里做错了什么?

Can anyone let me know what I did wrong here?

推荐答案

需要使用

return getNum(numList)

代替

getNum(numList)

getNum 函数中.原因是您递归调用 getNum 函数,因此您必须通过您所做的所有递归返回接受的值.此外,您必须将参数传递给每个调用.

in the getNum function. The reason is that you call the getNum function recursively, thus you have to return the accepted value back through all recursions you made. Additionally, you must pass the arguments to each call.

这篇关于Python获取用户输入错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆