简体   繁体   English

问题是找到第二大数字,我的代码有什么问题,我是初学者

[英]Question is Find the 2nd largest digit ,What is wrong with my code, I am a beginner

Question is Find the 2nd largest digit,What is wrong with my code, I am a beginner,Output is still zero问题是求第2大的数字,我的代码有什么问题,我是初学者,Output还是0

a = input("Enter your number")
max = 0
maxx  = 0

list1 = []


for i in a :
    list1.append(i)
    if i > str(max) :
        max = i
        list1.remove(max)
        for j in list1 :
            if j > str(maxx) :
            maxx = j
print(maxx)

The second for loop shouldn't have been nested and inside a conditional like that, since it would only run until the highest number was found.第二个for循环不应该嵌套在这样的条件语句中,因为它只会运行到找到最大数字为止。

The other main issue is that that even without the second for loop being nested, in a situation where a = '12345' every number is the highest the first loop finds and is therefore deleted from list1 meaning that list1 ends up completely empty.另一个主要问题是,即使没有嵌套第二个for循环,在a = '12345'的情况下,每个数字都是第一个循环找到的最高数字,因此从list1中删除,这意味着list1最终完全为空。

This should work:这应该有效:

a = input("Enter your number: ")
max_num = 0
maxx  = 0


for i in a :
    if i > str(max_num) :
        max_num = i
        
for j in a :
    if j > str(maxx) and j < str(max_num):
        maxx = j
        
print(maxx)

or you can also do this:或者你也可以这样做:

nums = input('Enter numbers: ')
list1 = []

for i in nums:
    list1.append(int(i))
    
list1.sort()
print(list1[-2])

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我的python code_非常初学者问题有什么问题吗? - What is the wrong with my python code_ very beginner question? 我的代码能解决问题吗? 我是python的初学者 - Does my code solve the question? I am a beginner in python 在我的计算中,要从两个3位数的乘积中得到最大的回文数,在哪里出错? - where am I going going wrong in my calculations to get largest palindrome made from the product of two 3-digit numbers? 不确定我的代码有什么问题(Tkinter BEGINNER) - Not sure what is wrong with my code (Tkinter BEGINNER) 我的代码找不到我的文件,我不确定它出了什么问题 - My code cannot find my file, and I am not sure what is wrong with it 这个 python 代码有什么问题? (初学者的问题) - What's wrong with this python code? (beginner's question) 在多维数组pyhton的行中找到最大,第二大或第三大的索引 - find the index of largest, 2nd largest or third largest in a row of multidimensional array pyhton 不知道为什么在我通过第二次测试时会出现错误,任何人都可以看到我在代码中的哪个位置遇到了这个问题吗? - Not sure why I am getting a error when I pass through the 2nd test can anyone see where in my code I am getting this issue? 我在这里做错了什么? (Python 3)(初学者) - What am I doing wrong here? (Python 3) (Beginner) 我的代码的 CSV 部分做错了什么 - What am I doing wrong with the CSV part of my code
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM