简体   繁体   English

我在使用Python函数时遇到麻烦,有人可以帮我吗?

[英]I am having troubles with Python functions, can someone help me?

Goal: I am trying to create a program that can take in 10 numbers then spit out the 10 largest numbers. 目标:我正在尝试创建一个程序,该程序可以输入10个数字,然后吐出10个最大的数字。

I need all integers able to be inserted, then for the program to find the odds and see which of them are the biggest. 我需要所有能够插入的整数,然后程序才能找到几率,并查看其中最大的几率。

#Introduction
print ('Enter 10 odd numbers to see which is the greatest ')
#The big question
user_input = raw_input ('Enter a odd number ')
#Input function that only accepts intergers
numbers = []
while numbers < 11:
    try:
        numbers.append(int(raw_input(user_input)))
        break
    except ValueError:
        print 'Invalid number'
#Function that finds the highest odd and sees if it is odd
highest_odd = max(user_input) and user_input % 2 != 0
print 'The largest odd number was' + str(highest_odd)

What you need to fix: 您需要解决的问题:

  1. Check whether the length of the list numbers exceeds 9 or not. 检查列表numbers长度是否超过9。 You can get the length of a list by using len( ) function. 您可以使用len()函数获取列表的长度。 So, it should be : while len(numbers) < 9: . 因此,应为: while len(numbers) < 9:

  2. You didn't append the first input to the list numbers . 您没有将第一个输入添加到列表numbers

  3. The way you find the highest odd doesn't work. find the highest odd的方法不起作用。 Check out the modification. 检查修改。

To sum up, the code should be : 综上所述,代码应为:

#Introduction
print ('Enter 10 odd numbers to see which is the greatest ')

#The big question
user_input = int(raw_input('Enter an odd number '))

#Input that only accepts integers
numbers = []
while len(numbers) < 9:
    try:
        numbers.append(user_input)
        user_input = int(raw_input('Enter an odd number '))
    except ValueError:
        print 'Invalid number'

#Find the highest odd
highest_odd = max(i for i in numbers if i % 2)

print "The largest odd number was " + str(highest_odd)

Sample : 样品:

>>> Enter 10 odd numbers to see which is the greatest 
>>> Enter an odd number 3
>>> Enter an odd number 5
>>> Enter an odd number 1
>>> Enter an odd number 7
>>> Enter an odd number 6
>>> Enter an odd number 4
>>> Enter an odd number 1.3
Invalid number

>>> Enter an odd number 9
>>> Enter an odd number 4
>>> Enter an odd number 6
The largest odd number was 9

暂无
暂无

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

相关问题 我对这个刽子手程序有点麻烦。 有人可以帮我弄清楚吗? - I am having a bit of trouble with this hangman program. Can someone help me figure it out? 有人可以帮我优化下面的 python 代码吗,我在某些测试用例中遇到超时异常 - Can someone help me in optimising the below python code, I am getting timeout exception for some of the test cases Python,我正在尝试制作排行榜,但它不会保存在文本文件中,有人可以帮助我吗? - Python, I am trying to make a leader board but it wont save in the text file can someone help me? 我希望有人可以在Python3(3.6.2)中提供正则表达式帮助我 - I am hoping someone can help me with a regular expression in Python3 (3.6.2) 我正在尝试用 python 制作一个测验应用程序,并想单独显示问题和选项有人可以帮助我吗? - I am trying to make a quiz app in python and want to display the question and options separately can someone help me? 您好,我是python新手,在将文件作为输入时遇到困难。 有人可以帮我吗? - Hello, I am new to python and I am having difficulties on having a file as an input. Can somebody help me please? 有人可以帮助我解决 FileNotFoundError python - someone can help me with FileNotFoundError python 有人可以帮助我进行Python的并行处理吗? - Can someone help me with parallel processing for Python? 有人可以帮我写代码吗? (Python) - Can someone help me with my code? (python) 有人可以帮我理解python中的循环吗 - Can someone help me understand this for loop in python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM