简体   繁体   English

我写了这段代码来找到最大和最小的(int)数,但它不起作用

[英]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.

相关问题 查找最大和最小数字代码不适用于 python - Find Largest and Smallest number code not working on python 如何使用不使用 min() 或 max() 的输入找到最小和最大数 - How can I find smallest and largest number with inputs not using min() nor max() 该代码并不总是给出不在列表中的最大和最小数字之间的正确最小数字 - The code doesn't always give correct smallest number between the largest and the smallest number that is not in the list 循环将最小的数字打印为最大的数字 - The loop prints the smallest number as the largest 使用python的最小和最大数 - smallest and largest number using python 我如何在 Python 中的列表中找到最小值或最大值 - How do i find the smallest or largest value in a list in Python 检查数组的最大和最小数 - Checking an array for largest and smallest number 如何以我在第一行中获得最大数字、在第二行中获得最小数字、在第三行中获得第二大数字的方式对组进行排序,依此类推 - How to sort a group in a way that I get the largest number in the first row and smallest in the second and the second largest in the third and so on 如何对两个参数进行排序 应该先显示最大的数字,然后从小到大 - How do I sort two parameters The largest number should be displayed first, and then from the smallest to the largest 您如何在几个阵列中找到最大/最小的数字? - How do you find the largest/smallest number amongst several array's?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM