[英]if statement, going back and forth
我是 python 的新手,但是已經編寫了一些結構化文本和一點點 c++ 現在我正在使用 if 語句解決一個問題,它似乎不像我習慣的那樣工作
示例代碼:
more_guests = 0
loop = bool; loop = False
ferdig = int; ferdig = 1
while loop == False:
guests = []
more_guests = 0
if int(ferdig) == 1:
guest = input("type in guest ")
more_guests = int(input("done? 1 for yes 2 for no "))
if int(more_guests) == 1:
guests.append(guest)
ferdig == 3
elif int(more_guests) == 2:
guests.append(guest)
ferdig == 2
else:
print("unvalid answer, please use 1 or 2")
ferdig == 1
elif int(ferdig) == 2:
ferdig = 1
elif int(ferdig) == 3:
print(guests)
else:
loop = True
我試過確保它是一個 integer 等等,只是一直卡在完成? 1 表示是 2 表示否,它總是讓我循環輸入客人
在使用結構化文本時,我經常使用這種來回的方式,但我似乎無法在 python 中理解它,也許我應該使用 case/switch? 無論如何,如果有人可以幫助我理解您是否可以在 python 中以這種方式使用 IF 語句,我們將不勝感激
我認為你寫錯了運算符。 ferdig == 1 只是比較值運算符。 所以它返回 true(1) 或 false(0)。
如果你想改變 ferdig 的值,你應該在 if 語句中寫 ferdig = 3。
我已經清理了你的代碼以獲得我認為你想要的東西。
guests = []
while True:
guest = input("type in guest ")
guests.append(guest)
response = int(input("done? 1 for yes 2 for no "))
if response == 1:
print(guests)
break
elif response == 2:
continue
elif response == 3:
print(guests)
else:
print("invalid answer, please use 1 or 2")
您的代碼有很多問題,這里已經修復:
more_guests = 0
# you don't need to declare types, but if you want to, this is how
loop: bool = False
# however, Python can just infer the type itself like this
ferdig = 1
# you don't want to reset guests every time around the loop
guests = []
while loop == False:
more_guests = 0
if int(ferdig) == 1:
guest = input("type in guest ")
# you always want to append a guest, including if someone types a 1 after
guests.append(guest)
more_guests = int(input("done? 1 for yes 2 for no "))
if int(more_guests) == 1:
ferdig = 3
elif int(more_guests) == 2:
ferdig = 2
else:
print("invalid answer, please use 1 or 2")
ferdig = 1
elif int(ferdig) == 2:
ferdig = 1
elif int(ferdig) == 3:
# after printing, you're done, you don't want to print forever
print(guests)
loop = True
請注意,您的大部分代碼並不是真正需要的,您正在做很多 Python 可以為您做的簿記工作,或者只是不需要:
# this isn't needed, because you set that at the start of the loop anyway
# more_guests = 0
# this isn't needed, because you can tell when to stop from ferdig
# loop: bool = False
# starting at 2, since that means you want to keep going
ferdig = 2
guests = []
while ferdig != 1:
# this isn't needed, you can just read ferdig
# more_guests = 0
# this isn't needed, you want a new guest on every loop
#if int(ferdig) == 1:
guest = input("type in guest ")
guests.append(guest)
ferdig = int(input("done? 1 for yes 2 for no "))
# none of this is needed, all you need to know is if ferdig is 1 or 2
# if int(more_guests) == 1:
# ferdig = 3
# elif int(more_guests) == 2:
# ferdig = 2
# else:
if ferdig not in (1, 2):
print("invalid answer, please use 1 or 2")
ferdig = 1
# this is also not needed, at this point ferdig will be 1 or 2
# elif int(ferdig) == 2:
# ferdig = 1
# elif int(ferdig) == 3:
# put the print outside the loop and it only prints once
print(guests)
# got rid of this
# loop = True
所以,這只是:
ferdig = 2
guests = []
while ferdig != 1:
guest = input("type in guest ")
guests.append(guest)
ferdig = int(input("done? 1 for yes 2 for no "))
if ferdig not in (1, 2):
print("invalid answer, please use 1 or 2")
ferdig = 1
print(guests)
最后,這將做同樣的事情:
more_guests = True
guests = []
while more_guests:
guests.append(input("type in guest "))
more_guests = input("done? 1 for yes") != '1'
print(guests)
這是使用函數完成您嘗試執行的操作的另一種選擇。
guests = []
def add_guest():
guest = input("type in guest ")
guests.append(guest)
add_more()
def add_more():
more_guests = int(input("done? 1 for yes 2 for no "))
#if they answer 1 we print out the final list
if more_guests == 1:
print("the guest list is {}".format(guests))
print('have a nice day')
#if they answer 2 we go to the add_guest function
elif more_guests == 2:
add_guest()
#if they answer anything other than 1 or 2 we re-call the add_more function.
else:
print("unvalid answer, please use 1 or 2")
add_more()
add_guest()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.