我無法弄清楚如何將我的代碼回圈回特定點,我有 2 個回圈點,第一個作業正常,但我無法讓第二個作業,因為我必須定義整數變數“ num_stores_int”但如果我這樣做了,那么“while”回圈就不起作用了。有關這些點的位置,請參閱我的代碼。
這是我的代碼:
num_stores = ("")
num_stores_int = int(num_stores)
while num_stores.isnumeric() == False:
while num_stores_int > 10: #This is where I want it to loop to
num_stores = input ("\n To start, please enter the amount of stores you own: ")
if num_stores.isnumeric() == True:
num_stores_int = int(num_stores)
if num_stores_int > 10: #this is where I want the "while" loop to validate the integer being less than 10
print (" Sorry, this sales tracker can track a maximum of 10 stores.\n Here, try that again, I'll reboot it for you.\n")
print (" -----------------------REBOOTING------------------------")
if num_stores_int >= 5:
print ("\n Hmm, interesting, you'd think someone with that many \n stores would be able to afford a better sales tracker.")
print (" Well, I guess you're stuck with me! MUHAHAHAHA!!......\n\n Anyway,")
print (f" clearly big business person, you own {num_stores_int} stores.\n I'm gonna need you to tell me where each one is")
else:
num_stores_int = int(num_stores)
print (f" Alright... so, random business person, you have {num_stores_int} stores.\n Now, I'm going to need you to tell me where each one is")
else:
print ("\n Hey, uhh, you're going to have to write a number for the\n amount of stores you've got, letters and decimals don't \n really work. Here, try again, I'll reboot it for you.\n")
print (" -----------------------REBOOTING------------------------")
uj5u.com熱心網友回復:
您在尋找什么不是很清楚,但我相信您的外while回圈是為了在用戶輸入非數字內容時不斷詢問用戶輸入?
我只會while在要求用戶輸入的代碼周圍包裝該回圈,如下所示:
num_stores = ("")
num_stores_int = 0
while num_stores_int < 10: #This is where I want it to loop to
num_stores = input ("\n To start, please enter the amount of stores you own:")
while num_stores.isnumeric() == False:
print ("\n Hey, uhh, you're going to have to write a number for the\n amount of stores you've got, letters and decimals don't \n really work. Here, try again, I'll reboot it for you.\n")
print (" -----------------------REBOOTING------------------------")
num_stores = input ("\n To start, please enter the amount of stores you own:")
num_stores_int = int(num_stores)
if num_stores_int > 10: #this is where I want the "while" loop to validate the integer being less than 10
print (" Sorry, this sales tracker can track a maximum of 10 stores.\n Here, try that again, I'll reboot it for you.\n")
print (" -----------------------REBOOTING------------------------")
elif num_stores_int >= 5:
print ("\n Hmm, interesting, you'd think someone with that many \n stores would be able to afford a better sales tracker.")
print (" Well, I guess you're stuck with me! MUHAHAHAHA!!......\n\n Anyway,")
print (f" clearly big business person, you own {num_stores_int} stores.\n I'm gonna need you to tell me where each one is")
else:
print (f" Alright... so, random business person, you have {num_stores_int} stores.\n Now, I'm going to need you to tell me where each one is")
uj5u.com熱心網友回復:
我的錯,我一定沒有很好地解釋出了什么問題,但我只是嘗試了一些東西并且它起作用了。
def restart():
num_stores = ("")
while num_stores.isnumeric() == False:
num_stores = input ("\n To start, please enter the amount of stores you own: ")
if num_stores.isnumeric() == True:
num_stores_int = int(num_stores)
if num_stores_int > 10:
print ("\n Sorry, this sales tracker can track a maximum of 10 stores.\n Here, try that again, I'll reboot it for you.\n")
print (" -----------------------REBOOTING------------------------")
restart()
elif num_stores_int >= 5:
print ("\n Hmm, interesting, you'd think someone with that many \n stores would be able to afford a better sales tracker.")
print (" Well, I guess you're stuck with me! MUHAHAHAHA!!......\n\n Anyway,")
print (f" clearly big business person, you own {num_stores_int} stores. I'm gonna\n need you to tell me where each one is")
else:
num_stores_int = int(num_stores)
print (f" Alright... so, random business person, you have {num_stores_int} stores.\n Now, I'm going to need you to tell me where each one is")
else:
print ("\n Hey, uhh, you're going to have to write a number for the\n amount of stores you've got, letters and decimals don't \n really work. Here, try again, I'll reboot it for you.\n")
print (" -----------------------REBOOTING------------------------")
restart()
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/365899.html
上一篇:如何驗證是否需要物件屬性?
