[英]How to check user input for multiple conditions within same loop or function in Python 3?
I'm trying to take user-input
but I need the input to be an integer, AND be between 1 and 9. I tried putting "in range(1,10)" in a few places in the code but it didn't work. 我正在尝试接受
user-input
但是我需要输入一个整数,并且介于1到9之间。我尝试在代码中的几个位置放置“ inrange(1,10)”,但没有工作。 I need the program to keep asking the user for the right input until they give the correct input. 我需要程序不断询问用户正确的输入,直到他们提供正确的输入为止。 So far I've only been able to make sure their input is an integer with he following code.
到目前为止,我只能使用下面的代码来确保它们的输入是整数。 I will be taking input by using
int(input("..."))
, rather than using input("...")
. 我将使用
int(input("..."))
而不是input("...")
。
while True:
try:
ui1 = int(input("Player 1, Your move. Select your move. "))
break
except ValueError:
print("You have to choose a number between 1 and 9")
continue
Why not just check isdigit()
and in range
? 为什么不只检查
isdigit()
和in range
呢?
while True:
ui1 = input("Player 1, Your move. Select your move. ")
if ui1.isdigit() and int(ui1) in range(1,10):
break
print("You have to choose a number between 1 and 9")
# Continue code out of the loop
Add a check before the break and move the error message to the end of the loop. 在中断之前添加检查,然后将错误消息移至循环末尾。
while True:
try:
ui1 = int(input("Player 1, Your move. Select your move. "))
if 1 <= ui1 <= 9:
break
except ValueError:
pass
print("You have to choose a number between 1 and 9")
# beJeb
# Stack overflow -
# https://stackoverflow.com/questions/51202856/how-to-check-user-input-for-multiple-conditions-within-same-loop-or-function-in
# Our main function, only used to grab input and call our other function(s).
def main():
while True:
try:
userVar = int(input("Player 1, Your move. Select your move: "))
break
except ValueError:
print("Incorrect input type, please enter an integer: ")
# We can assume that our input is an int if we get here, so check for range
checkRange = isGoodRange(userVar)
# Checking to make sure our input is in range and reprompt, or print.
if(checkRange != False):
print("Player 1 chooses to make the move: %d" %(userVar))
else:
print("Your input is not in the range of 1-9, please enter a correct var.")
main()
# This function will check if our number is within our range.
def isGoodRange(whatNum):
if(whatNum < 10) & (whatNum > 0):
return True
else: return False
# Protecting the main function
if __name__ == "__main__":
main()
Note: I tested a couple inputs so I believe this should be enough to help you understand the process, if not please comment, message, etc. Also, if this answer helps you, please select it as answered to help others. 注意:我测试了几个输入,因此我认为这应该足以帮助您理解该过程,如果不能,请发表评论,消息等。此外,如果此答案对您有所帮助,请选择它作为回答来帮助其他人。
while(1): #To ensure that input is continuous
number = int(input())
if number>=1 and number<=10 and number.isdigit():
break #if the input is valid, you can proceed with the number
else:
print("Enter a valid Number")
The number can be used for further operations. 该号码可用于进一步的操作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.