如果與特定串列中的字串值不匹配,我無法要求用戶重新輸入輸入。我錯過了什么?
El_List = 'H', 'He', 'Li', 'Be', 'B', 'C', 'N', 'O', 'F', 'Ne'
user_instructions = ( 'Select Elements value from following list to create a molecule')
print (user_instructions )
while true :
try :
elem = str(input('add molecular formula: '))
if elem = 'H' or = 'He' or = 'Li' or = 'Be' or = 'B' or = 'C' or = 'N' or = 'O' or = 'F' or = 'Ne':
print (elem)
break,
else :
print ('Not Found in Element list)
except: ValueError :
print('add molecular formula: ')
continue
uj5u.com熱心網友回復:
歡迎來到 SO。首先,您的代碼中有很多縮進錯誤。我只是在清理代碼后嘗試過,并且作業正常。
嘗試這個:
El_List = 'H', 'He', 'Li', 'Be', 'B', 'C', 'N', 'O', 'F', 'Ne'
user_instructions = ( 'Select Elements value from following list to create a molecule')
print (user_instructions)
while True:
try :
elem = str(input('add molecular formula: '))
if elem == 'H' or elem == 'He' or elem == 'Li' or elem == 'Be' or elem == 'B' or elem == 'C' or elem == 'N' or elem == 'O' or elem == 'F' or elem == 'Ne':
print(elem)
break
else :
print ('Not Found in Element list')
except ValueError :
print('add molecular formula: ')
continue
您的 except 在您的 try 陳述句中。確保所有的縮進都是正確的。希望這可以幫助!
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/525172.html
上一篇:VBA回圈崩潰Excel
