【发布时间】:2021-05-07 10:38:26
【问题描述】:
我买了一本书来自学使用 Python 编程。我目前不参加任何在线课程。我在第 2 章中,并且在练习中遇到问题。我要编写一个程序,要求输入 10 个整数,然后打印最大的奇数。如果没有输入奇数,它应该打印一条消息。
x = 0
largest_odd = int()
while(x < 10):
user_input = int(input('Enter an integer '))
if user_input%2 != 0 and user_input > largest_odd:
largest_odd = user_input
elif user_input%2 == 0 and x == 10:
print('no odd numbers')
x += 1
print(f'the largest odd number is {largest_odd}')
如果不打印最后一个打印语句,我很难输入所有偶数。我知道最后一个打印语句将打印,因为它在循环之外。但过去几个小时我一直在做这个,不知道我应该改变什么。
请帮忙。
【问题讨论】:
标签: python-3.x while-loop break control-flow