拒绝字母,只允许输入数字@在Python 3.x中输入第一个和第二个数字

时间:2019-03-31 22:57:32

标签: python-3.x validation input

尝试仅允许在python 3.x中输入数字而不输入字母,并要求用户输入数字(如果输入字母)。我有两个数字需要输入,输入时必须拒绝它们。

print ('Hello There, what is your name?') # String asks the user for their name
myname = input() #string
print ('Nice to meet you, '+ myname)# String responds "Nice to meet you" and input the users name that was entered
print() #adds a space
print ('We want to some math today!') # String tells user we want to do some math today
print() # adds a space
num1,num2 = float(input("Enter first number")), float(input("Enter second number"))
sum = num1+num2
if sum  >100 : # Determines if users inputs when added together are  > 100
 print('They add up to a "Big Number" ') # If inputs are > 100 prints "They add up to a big number"
 # and the users name
elif sum <=100 : # Determines if the users inputs when added are = to or <100
 print ('They add up to' ,(sum))

2 个答案:

答案 0 :(得分:0)

在验证多个输入时,我通常会创建一个单独的函数来进行验证。尽管在这里可能没有必要,但是将重复的代码排除在外是一个好习惯。如果两个输入都不是浮点的,它将退出。这是我的方法,使用带有ValueError异常的try块。顺便说一句,换行可以通过在所需字符串的末尾加上'\ n'来实现。使用print()是一种草率的方法。

import sys
def checkNum(number):
    try:
        number = float(number)
    except ValueError:
        print("That is not a float.")
        sys.exit()
    return number    

然后,您可以像这样使用原始代码:

print ('Hello There, what is your name?') #String asks the user for their name
myname = input() #string
print ('Nice to meet you, '+ myname + '\n')#String responds "Nice to meet you" and input the users name that was entered
print ('We want to some math today!\n') #String tells user we want to do some math today
num1 = checkNum(input("Enter first number"))
num2 = checkNum(input("Enter second number"))
sum = num1+num2
if sum  >100 : # Determines if users inputs when added together are  > 100
    print('They add up to a "Big Number" ') #If inputs are > 100 prints "They add up to a big number" and the users name
elif sum <=100 : #Determines if the users inputs when added are = to or <100
    print ('They add up to' ,(sum))

答案 1 :(得分:0)

您需要一个 while循环,以连续接受有效输入,在您的情况下,该输入是数字

另一件事是,您需要检查是输入的数字输入还是输入的输入,在这种情况下,您可以使用Python内置的 isdigit()功能来做到这一点。

最后,在添加两者以避免输入键相关的错误时,请键入强制转换输入浮动 int

print ('Hello there, what is your name?') # String asks the user for their name
myname = input() #string
print ('Nice to meet you, '+ myname) # String responds "Nice to meet you" and input the users name that was entered
print() #adds a space
print ('We want to some math today!') # String tells user we want to do some math today
print() # adds a space

i = False
while i == False:
    num1 = input("Enter first number: ")
    if num1.isdigit():
        i = True
    else:
        i = False

print() # adds a space

j = False
while j == False:
    num2 = input("Enter Second number: ")
    if num2.isdigit():
        j = True
    else:
        j = False

sum = float(num1) + float(num2)
if sum  > 100 : # Determines if users inputs when added together are  > 100
    print('They add up to a "Big Number" ') # If inputs are > 100 prints "They add up to a big number"
 # and the users name

elif sum <= 100 : # Determines if the users inputs when added are = to or <100
    print ('They add up to' ,(sum))

如果这对您有用,请告诉我!