Python 无法将一个变量的值分配给另一个变量
我正在试着写一段代码 -哪个输入一个数字 -如果其最大值替换为变量large的值(当前设置为最大值=无) -对于最小的 -输入“完成”以停止代码并打印最大值和最小值 代码如下Python 无法将一个变量的值分配给另一个变量,python,Python,我正在试着写一段代码 -哪个输入一个数字 -如果其最大值替换为变量large的值(当前设置为最大值=无) -对于最小的 -输入“完成”以停止代码并打印最大值和最小值 代码如下 largest = None smallest = None while True: num = input("Enter a number: ") try: num = int(num) #captcha for user if he enters anything e
largest = None
smallest = None
while True:
num = input("Enter a number: ")
try:
num = int(num) #captcha for user if he enters anything else than integer,
#it won't convert to int and move to except
if largest < num:
largest = num
continue
elif smallest > num:
smallest = num
continue
except:
try:
if num == "done" :break
except:
print ("Invalid input")
continue
print ("Maximum", largest)
print ("Minimum", smallest)
但它只是说
Maximum None
Minimum None
您试图将
None
与具有小于
和大于code>运算符的int
进行比较,但该操作无效。您可能应该将最小的
和最大的
实例化为数字。我没有测试此代码,但尝试修复错误:
largest = None
smallest = None
while True:
v = input("Enter a number: ")
try:
num = int(v)
if largest in None or largest < num:
largest = num
continue
if smallest is None or smallest > num:
smallest = num
continue
except ValueError:
if v == "done":
break
print ("Invalid input")
print ("Maximum:", largest)
print ("Minimum:", smallest)
max=None
最小=无
尽管如此:
v=输入(“输入一个数字:”)
尝试:
num=int(v)
如果无中最大或最大数值:
最小=num
持续
除值错误外:
如果v==“完成”:
打破
打印(“无效输入”)
打印(“最大:”,最大)
打印(“最小:”,最小)
您无法将None
与数字进行比较。将第一个数字保留为最大值和最小值,然后比较其余数字。为此,请使用计数器c
max=0
最小值=0
c=0
尽管如此:
num=输入(“输入一个数字:”)
尝试:
num=int(num)#如果用户输入的不是整数,则为其验证码,
#它不会转换为int并移动到except
如果c==0:
最小=num
最大=num
elif最大数值:
最小=num
持续
除:
尝试:
如果num==“完成”:中断
除:
打印(“无效输入”)
持续
c+=1
打印(“最大”,最大)
打印(“最小值”,最小值)
这应该有帮助!
这将创建一个列表并仅向列表中添加整数,最后打印最大值和最小值
largest = 0
smallest = 0
flag = True
while True:
try:
inputValue = input("Enter value: ")
if inputValue == "Done":
flag = False
break
if type(int(inputValue)) is int:
inputValue = int(inputValue)
if largest < inputValue:
largest = inputValue
continue
elif smallest > inputValue:
smallest = inputValue
continue
except:
print("Invalid input")
continue
print("Maximum: ", largest)
print("Minimum: ", smallest)
最小值=0
flag=True
尽管如此:
尝试:
inputValue=输入(“输入值:”)
如果inputValue==“完成”:
flag=False
打破
如果类型(int(inputValue))为int:
inputValue=int(inputValue)
如果最大值<输入值:
最大值=输入值
持续
elif最小>输入值:
最小值=输入值
持续
除:
打印(“无效输入”)
持续
打印(“最大:”,最大)
打印(“最小:”,最小)
您无法将
None
与整数进行比较,因此您的if
语句总是导致TypeError
异常。
list1 = []
while True:
num = input("Enter a number: ")
try:
num = int(num) #captcha for user if he enters anything else than integer,
#it won't convert to int and move to except
list1.append(num)
except:
try:
if num == "done" :break
except:
print ("Invalid input")
continue
print ("Maximum", max(list1))
print ("Minimum", min(list1))
largest = 0
smallest = 0
flag = True
while True:
try:
inputValue = input("Enter value: ")
if inputValue == "Done":
flag = False
break
if type(int(inputValue)) is int:
inputValue = int(inputValue)
if largest < inputValue:
largest = inputValue
continue
elif smallest > inputValue:
smallest = inputValue
continue
except:
print("Invalid input")
continue
print("Maximum: ", largest)
print("Minimum: ", smallest)