如何在Python3.5上创建一个用户输入10个整数的程序

如何在Python3.5上创建一个用户输入10个整数的程序,python,loops,integer,Python,Loops,Integer,用户将输入10个整数,作为回报,程序将告诉用户输入的10个整数中哪一个是最大的奇数整数。我已经在下面粘贴了我工作的脚本。我的工作的问题是,当最大的数字是偶数时,它返回一个错误,我如何让python忽略最大的偶数并继续下一个最大的奇数 n1 = int(input('enter integer 1: ')) n2 = int(input('enter integer 2: ')) n3 = int(input('enter integer 3: ')) n4 = int(input('enter

用户将输入10个整数,作为回报,程序将告诉用户输入的10个整数中哪一个是最大的奇数整数。我已经在下面粘贴了我工作的脚本。我的工作的问题是,当最大的数字是偶数时,它返回一个错误,我如何让python忽略最大的偶数并继续下一个最大的奇数

n1 = int(input('enter integer 1: '))
n2 = int(input('enter integer 2: '))
n3 = int(input('enter integer 3: '))
n4 = int(input('enter integer 4: '))
n5 = int(input('enter integer 5: '))
n6 = int(input('enter integer 6: '))
n7 = int(input('enter integer 7: '))
n8 = int(input('enter integer 8: '))
n9 = int(input('enter integer 9: '))
n10 = int(input('enter integer 10: '))

if n1%2!=0:
    a=n1
if n2%2!=0:
    b=n2
if n3%2!=0:
    c=n3
if n4%2!=0:
    d=n4
if n5%2!=0:
    e=n5
if n6%2!=0:
    f=n6
if n7%2!=0:
    g=n7
if n8%2!=0:
    h=n8
if n9%2!=0:
    i=n9
if n10%2!=0:
    j=n10

if n1>n2 and n1>n3 and n1>n4 and n1>n5 and n1>n6 and n1>n7 and n1>n8 and n1>n9 and n1>n10:
    print(n1, 'is the largest odd number')
if n2>n1 and n2>n3 and n2>n4 and n2>n5 and n2>n6 and n2>n7 and n2>n8 and n2>n9 and n2>n10:
    print(b, 'is the largest odd number')
if n3>n1 and n3>n2 and n3>n4 and n3>n5 and n3>n6 and n3>n7 and n3>n8 and n3>n9 and n3>n10:
    print(c, 'is the largest odd number')
if n4>n1 and n4>n2 and n4>n3 and n4>n5 and n4>n6 and n4>n7 and n4>n8 and n4>n9 and n4>n10:
    print(d, 'is the largest odd number')
if n5>n1 and n5>n2 and n5>n3 and n5>n4 and n5>n6 and n5>n7 and n5>n8 and n5>n9 and n5>n10:
    print(e, 'is the largest odd number')
if n6>n1 and n6>n2 and n6>n3 and n6>n4 and n6>n5 and n6>n7 and n6>n8 and n6>n9 and n6>n10:
    print(f, 'is the largest odd number')
if n7>n1 and n7>n2 and n7>n3 and n7>n4 and n7>n5 and n7>n6 and n7>n8 and n7>n9 and n7>n10:
    print(g, 'is the largest odd number')
if n8>n1 and n8>n2 and n8>n3 and n8>n4 and n8>n5 and n8>n6 and n8>n7 and n8>n9 and n8>n10:
    print(h, 'is the largest odd number')
if n9>n1 and n9>n2 and n9>n3 and n9>n4 and n9>n5 and n9>n6 and n9>n7 and n9>n8 and n9>n10:
    print(i, 'is the largest odd number')
if n10>n1 and n10>n2 and n10>n3 and n10>n4 and n10>n5 and n10>n6 and n10>n7 and n10>n8 and n10>n9:
    print(j, 'is the largest odd number')

我不完全确定您遇到了什么问题以及出现了什么错误,但您应该真正考虑使用循环。像这样:

odd_numbers = []
for i in range(1, 11):
    input = int(input('enter integer {}: '.format(i)))
    if input % 2:
        odd_numbers.append(input)

if odd_numbers:
    print('{} is the largest odd number'.format(max(odd_numbers)))
else:
    print('There were no odd numbers entered')

通常情况下,我会将此作为一条评论来撰写,但我想帮助OP了解如何使用循环

我不完全确定您遇到了什么问题以及出现了什么错误,但您应该真正研究使用循环。像这样:

odd_numbers = []
for i in range(1, 11):
    input = int(input('enter integer {}: '.format(i)))
    if input % 2:
        odd_numbers.append(input)

if odd_numbers:
    print('{} is the largest odd number'.format(max(odd_numbers)))
else:
    print('There were no odd numbers entered')

通常情况下,我会将此作为注释编写,但想帮助OP了解如何使用循环

您的代码的许多问题中的第一个问题是,当您尝试打印它时,
j
可能没有定义

话虽如此,您的代码确实需要重构。查看列表、循环、生成器和
max
函数。开始尝试修复代码是毫无意义的

以下是一个简明版本的建议:

def max_odd():
    prompt = 'enter integer {}: '
    entered = (int(input(prompt.format(i+1))) for i in range(10))
    try:
        return max(x for x in entered if x%2)
    except ValueError: # no odd number
        return None
演示:


代码的许多问题中的第一个问题是,当您尝试打印代码时,
j
可能没有定义

话虽如此,您的代码确实需要重构。查看列表、循环、生成器和
max
函数。开始尝试修复代码是毫无意义的

以下是一个简明版本的建议:

def max_odd():
    prompt = 'enter integer {}: '
    entered = (int(input(prompt.format(i+1))) for i in range(10))
    try:
        return max(x for x in entered if x%2)
    except ValueError: # no odd number
        return None
演示:


考虑做如下事情:

maximum=0 #Variable to store the maximum. Since this is even, if we get to the end of the program and it's still 0, we can be sure there were no odd numbers entered.
for i in range(1,11): #Loop from i=1 to i=10
    n=int(input 'enter integer: {}'.format(i)) #Ask for an integer input
    if (n%2) != 0: #If the number is odd
        if maximum==0 or n>maximum: #Then if it is bigger than the old maximum or it's the first odd number
            maximum=n #Then set it as the new maximum
if (maximum != 0): #If any odd numbers were entered
    print(maximum, ' is the largest odd number') #Output the biggest
else: #If none of the numbers were odd
    print('all input numbers were even') #Then complain

考虑做如下事情:

maximum=0 #Variable to store the maximum. Since this is even, if we get to the end of the program and it's still 0, we can be sure there were no odd numbers entered.
for i in range(1,11): #Loop from i=1 to i=10
    n=int(input 'enter integer: {}'.format(i)) #Ask for an integer input
    if (n%2) != 0: #If the number is odd
        if maximum==0 or n>maximum: #Then if it is bigger than the old maximum or it's the first odd number
            maximum=n #Then set it as the new maximum
if (maximum != 0): #If any odd numbers were entered
    print(maximum, ' is the largest odd number') #Output the biggest
else: #If none of the numbers were odd
    print('all input numbers were even') #Then complain

它返回一个错误-我们必须猜测报告了什么错误,或者您会告诉我们吗?尝试阅读列表和循环。它们会使你的程序变得非常简单,返回时会出现错误-我们必须猜测报告的错误是什么,还是你会告诉我们?试着阅读列表和循环。它们将使您的程序更加简单注意:如果没有奇数输入,将引发ValueError。注意:如果没有奇数输入,将引发ValueError。