反复向用户请求值,直到满足条件为止



我正试图编写一个函数,如果整数n为正数和奇数,则返回True(否则为False(。然后使用此函数反复要求用户输入一个正整数和奇数整数,直到提供了五次(正整数和奇整数(,并在满足正/奇标准后返回该值。

无论是真是假,我都可以让它返回,但我被其他东西卡住了。

以下是我尝试过的:

x = int(input("enter a number: "))
values = [5]
def isOdd(x):
if x % 2 != 0 and x>0:
return True
print("yup")
else:
return False
print("no")

'''while count = <5 and x % 2 != 0 and x>0: #this is a different approach I tried
count = sum x+1
print ("no")
x = int(input("enter a number: "))'''

for i in range(x):
values.append(int(input('Please enter a value')))

我在这里错过了什么?我是编程新手,在计算函数和追加时有点麻烦。

已将代码更改为:

x = int(input("enter a number: "))
def isOdd(x):
if x % 2 != 0 and x>0:
return True
print("yup")
else:
return False
print("no")

while count <=5 and not isOdd(x):
count = sum x+1
print ("no")
x = int(input("enter a number: "))

现在得到这个错误:

line 12
print("yup")
^
IndentationError: unindent does not match any outer indentation level

你非常接近。你只需要用输入值调用你的函数,然后使用结果:

# Stay in this loop until 5 wrong guesses, or one right guess.
while count <=5 and not isOdd(x):

最新更新