Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3.x 在Python3.x中输入第一个和第二个数字时,拒绝字母,只允许数字输入_Python 3.x_Validation_Input - Fatal编程技术网

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

Python 3.x 在Python3.x中输入第一个和第二个数字时,拒绝字母,只允许数字输入,python-3.x,validation,input,Python 3.x,Validation,Input,尝试在Python3.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

尝试在Python3.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))
print('你好,你叫什么名字?)#String询问用户的姓名
myname=input()#字符串
打印(‘很高兴认识你,+myname)#字符串响应“很高兴认识你”,并输入输入的用户名
print()#添加一个空格
print(“我们今天想做一些数学!”)#字符串告诉用户我们今天想做一些数学
print()#添加一个空格
num1,num2=浮点(输入(“输入第一个数字”)),浮点(输入(“输入第二个数字”))
总和=num1+num2
如果总和>100:#确定用户输入相加时是否>100
打印(“它们加起来是一个“大数字”)#如果输入大于100个打印,“它们加起来是一个大数字”
#和用户名

elif sum当您验证多个输入时,我通常使用单独的函数来验证。虽然这里可能没有必要,但排除重复代码是一个好习惯。如果其中一个输入不是浮动,它将退出。下面是我将如何做的,使用带有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))
print('你好,你叫什么名字?)#String询问用户的姓名
myname=input()#字符串
打印(“很高兴认识你,+myname+”\n”)#字符串响应“很高兴认识你”,并输入输入的用户名
print('We want to some math today!\n')#字符串告诉用户我们今天要做一些数学
num1=checkNum(输入(“输入第一个数字”))
num2=checkNum(输入(“输入第二个数字”))
总和=num1+num2
如果总和>100:#确定用户输入相加时是否>100
打印(“它们加起来是一个“大数字”)#如果输入大于100个打印,“它们加起来是一个大数字”和用户名

elif sum您需要一个while循环来持续接受一个有效输入,在您的情况下,该输入是一个数值输入

另一件事是,您需要检查输入的输入是否为数字或,在这种情况下,您可以使用Python内置的isdigit()函数来执行此操作

最后,输入castfloatint,同时添加这两个选项以避免与str相关的错误

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))
print('你好,你叫什么名字?)#String询问用户的姓名
myname=input()#字符串
打印(‘很高兴认识你,+myname)#字符串响应“很高兴认识你”,并输入输入的用户名
print()#添加一个空格
print(“我们今天想做一些数学!”)#字符串告诉用户我们今天想做一些数学
print()#添加一个空格
i=假
当i==False时:
num1=输入(“输入第一个数字:”)
如果num1.isdigit():
i=正确
其他:
i=假
print()#添加一个空格
j=假
当j==False时:
num2=输入(“输入第二个数字:”)
如果num2.isdigit():
j=真
其他:
j=假
总和=浮点数(num1)+浮点数(num2)
如果总和>100:#确定用户输入相加时是否>100
打印(“它们加起来是一个“大数字”)#如果输入大于100个打印,“它们加起来是一个大数字”
#和用户名
伊里夫和