![](/img/trans.png)
[英]I am having a bit of trouble with this hangman program. Can someone help me figure it out?
[英]I am having troubles with Python functions, can someone help me?
目標:我正在嘗試創建一個程序,該程序可以輸入10個數字,然后吐出10個最大的數字。
我需要所有能夠插入的整數,然后程序才能找到幾率,並查看其中最大的幾率。
#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)
您需要解決的問題:
檢查列表numbers
的長度是否超過9。 您可以使用len()函數獲取列表的長度。 因此,應為: while len(numbers) < 9:
您沒有將第一個輸入添加到列表numbers
。
您find the highest odd
的方法不起作用。 檢查修改。
綜上所述,代碼應為:
#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)
樣品:
>>> 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.