Write a program that asks the user to input 10 integers, and then prints the largest odd number that was entered. If no odd number was entered, it should print a message to that effect.
我需要在不使用数组、异常、导入、列表或for循环的情况下完成它。只能使用条件语句和while循环。我写了下面的代码,但我觉得我的算法太长了。这个程序能用更简洁的方式写吗?我问这个问题的目的是想看看是否有更“Pythonic”的方法来编写同一个程序,我在下面提供了这个程序的代码。在s = (input('Enter 10 numbers: '))
t = '' # initialize empty "number" string
x = len(s)-1
y = 0 # initialize checked integer
z = 0 # initialize preceding integer
i = 0 # initialize counter
while i <= x:
print('i=', i)
while i <= x and s[i] != ' ':
t = t + s[i] # concatenate "string number"
i = i+1
if len(t) > 0: # to not run out of string index
y = int(t)
if y%2 != 0: # compare odd numbers
if y > z:
z = y
if y < z and z%2 == 0: # if y is a negative odd integer
z=y
if i <= x and s[i] == ' ':
i=i+1
t = '' # reset the "number" string
if z == 0:
print('No odd numbers entered')
else:
print('The largest odd number entered is', z)