如果用戶沒有輸入逗號,我如何讓用戶知道在用戶進入下一步之前輸入逗號?獲取用逗號分隔的兩個輸入
while True:
try:
x, y = input("Enter x and y").split(",")
break
except ValueError:
print("give me good input")
因此,如果用戶輸入:nocomma 我需要打印出錯誤說:你必須輸入x和y用逗號
如果用戶沒有輸入逗號,我如何讓用戶知道在用戶進入下一步之前輸入逗號?獲取用逗號分隔的兩個輸入
while True:
try:
x, y = input("Enter x and y").split(",")
break
except ValueError:
print("give me good input")
因此,如果用戶輸入:nocomma 我需要打印出錯誤說:你必須輸入x和y用逗號
我不知道你需要什麼分離,但解決方案是這樣的:
while True:
try:
val = input("Enter x and y")
x, y = str(val).split(",")
break
except ValueError:
print("give me good input")
因此,如果用戶輸入:nocomma我需要打印出錯誤說:你必須輸入x和y用逗號隔開 –
你可以重寫你的輸入提示說「請輸入x和y,用逗號分隔」和錯誤消息說「請用一個逗號分隔值」。
完整的代碼,你想:
val = ""
while (str(val).split(",") != 2):
val = input("Enter x and y: ")
try:
x, y = str(val).split(",")
break
except:
print("give me good input")
請發表您的現有代碼 –