在python小型应用程序上执行2整数和


def numberinput(num1, num2):
try:
int(num1)
int(num2)
return True
except:
return False
def add(num1, num2):
return int(num1) + int(num2)
def main():
import sys
var1 = input("enter your first number")
var2 = input("enter your first number")
if(numberinput(var1, var2)):
print(add(var1,var2))
else:
print("Invalid Input")
return
main()

在python小型应用程序上执行2个整数的和,同时验证整数不得超过字节编码整数的实际大小,即基数2中的最小00000000和最大11111111,或基数10中的最小0和最大255(对于正整数(

number输入函数应检查四个条件。所以,它的身体应该像

return num1<255 and num1>0 and num2<255 and num2>0

在更具体的python风格中,您可以编写

return 0<num1<255 and 0<num2<255

更改验证函数如下:

def numberinput(num1, num2):
return 0<=num1<=255 and 0<=num2<=255

我有一段旧代码,您可以将其用作参考,以帮助解决最小-最大问题。

cheese_max = 112.0
cheese_min = 0.16
price_per_one = 0.75
order_amount = input('How much cheese would you like?: ')
if float(order_amount) < cheese_min:
print('Invalid Entry: Please order more than our minimum of 0.75 units.')
elif float(order_amount) > cheese_max:
print('Invalid Entry: Please order less than our maximum of 112.0 units.')
else:
print('That will be',float(order_amount)*price_per_one)

如果你把它放在一个永远的循环中(而True:(,在更改它以满足你的最小值0和最大值11111111并对你的目的做出适当的响应后,它将只接受满足你的最大值和最小值要求的有效条目。

我真的不确定你想问什么,你的帖子里没有任何问题。但是,如果你在最小-最大条件下寻求帮助,这应该会有所帮助。

最新更新