我正在嘗試創建一個可重復的函式,該函式使用 while 回圈反復嘗試 try-except 回圈,但我在某些部分遇到了問題。這是我目前的功能:
def trytrytryagain(input):
ValueError
while ValueError:
try:
input() = int(input())
except ValueError:
print("You must enter a number")
input = int(input())
當我運行代碼并輸入“a”(以測驗它是否反復要求用戶輸入數字)時,它總是在第一次迭代后顯示此錯誤訊息。
Traceback (most recent call last):
File "main.py", line 7, in trytrytryagain
int(input())
ValueError: invalid literal for int() with base 10: 'a'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "main.py", line 16, in <module>
trytrytryagain (times_table)
File "main.py", line 10, in trytrytryagain
int(input())
ValueError: invalid literal for int() with base 10: 'a'
因此,我發現創建此功能非常困難。它意味著一直運行直到用戶輸入一個數字,并在每次迭代后顯示訊息“您必須輸入一個數字”。我很困惑,所以這是背景關系的完整代碼(它是一個時間表生成器)。
from time import sleep
def trytrytryagain(input):
ValueError
while ValueError:
try:
int(input())
except ValueError:
print("You must enter a number")
int(input())
print("Please input the times table you wish to complete (1, 2, 3, etc.).")
times_table = input
trytrytryagain (times_table)
print("Would you like to go up to another times table (do the 1 to 12 times tables)? yes/no")
try:
othertables = str(input()).lower()
except ValueError:
print("You must enter either yes or no")
othertables = str(input()).lower()
if othertables == "yes":
print("Enter which time table you want to go up to.")
try:
other_times_table = int(input())
except ValueError:
print("You must enter a number")
other_times_table = int(input())
print("Enter the maximum table you would like to go up to. (if you do the 3 to 5 times tables, what times table would you like to finish on - type 12 for 5 x 12, 13 for 5 x 13, etc.)")
try:
max_value = int(input())
except ValueError:
print("You must enter a number")
max_value = int(input())
for x2 in range(times_table, other_times_table 1):
for x in range(max_value 1):
print(f"{x} x {x2} =")
input()
sleep(0.1)
else:
print("Okay.")
print("Enter the maximum table you would like to go up to. (if you do the 3 to 5 times tables, what times table would you like to finish on (type 12 for 5 x 12, etc.))")
try:
max_value = int(input())
except ValueError:
print("You must enter a number")
max_value = int(input())
for x in range(times_table, max_value 1):
answer = x * times_table
print(f"{x} times {times_table} is {answer}")
sleep(0.1)
uj5u.com熱心網友回復:
您可以使這更簡單,只需捕獲和忽略錯誤并回圈直到沒有例外:
def trytrytryagain():
while True:
try:
print("You must enter a number")
return int(input())
except ValueError:
pass
print("You entered:", trytrytryagain())
uj5u.com熱心網友回復:
這是因為您正在input例外處理中呼叫該函式。
在第一個輸入上,例外被捕獲,但在第二個輸入上,我們在處理程式內部,沒有任何東西捕獲它。
如果你必須使用例外,試試這個:
while True:
try:
print("Please enter a number")
var = int(input())
break
except ValueError:
pass
uj5u.com熱心網友回復:
你可以使用這樣的東西:
def function():
while True:
try:
return int(input())
except ValueError:
print("You must enter a number")
input_value = function()
print(input_value)
uj5u.com熱心網友回復:
這是一個可以幫助您的小功能:
def getInput(prompt, t=str):
while True:
v = input(f'{prompt}: ')
try:
return t(v)
except ValueError:
print('Invalid input', file=sys.stderr)
用法示例:
S = getInput('輸入任意字串') # 這將回傳一個字串
I = getInput('輸入一個整數', int) # 這將回傳一個 int
F = getInput('輸入一個浮點數', float) # 這將回傳一個浮點數
當然,第二個引數可以是任何將字串作為輸入并進行某種處理的函式
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/370832.html
