[英]i wrote this code to find the largest and smallest (int)number but it does not work
I wrote this code to get an input of several int numbers and write the smallest and largest of them but the code does not work.我编写了这段代码来获取几个 int 数字的输入,并编写其中最小和最大的,但代码不起作用。
numbers=[]
num=input('enter your number')
Int_num=int(num)
Int_num.append(numbers)
print('maximum number is:',max(numbers))
print('minimum number is:',min(numbers))
In order to get a sequence of numbers:为了得到一个数字序列:
numbers = []
while True:
number = input('Enter a number or enter q to exit: ')
if number == 'q':
break
else:
numbers.append(int(number))
print(f'Max: {max(numbers)}, Min: {min(numbers)}')
Replace Int_num.append(numbers)
with numbers.append(Int_num.append)
将
Int_num.append(numbers)
替换为numbers.append(Int_num.append)
To get multiple numbers you can try:要获得多个号码,您可以尝试:
numbers = []
last_number = input('Enter a number: ')
while last_number:
numbers.append(int(last_number))
print(f'Max: {max(numbers)}, Min: {min(numbers)}')
You are trying to append a list to a integer, it should be numbers.append(Int_num)
, so you will append the number Int_num
to the list numbers
.您正在尝试 append 到 integer 的列表,它应该是
numbers.append(Int_num)
,所以您将 append 到列表中的numbers
Int_num
。
Try this:尝试这个:
numbers=[]
num=input('enter your number')
while num != "":
Int_num=int(num)
numbers.append(Int_num)
num=input('enter your number')
print('maximum number is:',max(numbers))
print('minimum number is:',min(numbers))
The problem is line Int_num.append(numbers)问题是行 Int_num.append(numbers)
it should be numbers.append(Int_num)它应该是 numbers.append(Int_num)
Also if you put a while(True): around all but the first line you can add lots of numbers to the list (use ctrl+c to stop the program, if it's running in cmd or powershell)另外,如果您放了一段时间(True):除了第一行之外的所有内容,您都可以在列表中添加大量数字(如果它在 cmd 或 powershell 中运行,请使用 ctrl+c 停止程序)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.