何も入力せずに「Enter」を押しても問題ありません。それはエラーを示しています!有効な入力が与えられるまでユーザーに再度入力を求める方法はありますか?具体的には、「Enter」だけが押されたかどうかをユーザに再度問い合わせる。 Python:何も入力せずに「Enter」キーが押された場合に再度入力を求めます。
def dice():
user = input("Do you want to roll the dice? ")
while user[0].lower() == 'y':
num = randrange(1, 7)
print("Number produced: ", num)
user = input("Do you want to roll the dice? ")
ループが何度も何度もお聞きしますが、 "入力" を押すと、エラーを以下を
Do you want to roll the dice?
Traceback (most recent call last):
File "C:/Users/a/Documents/Code/Learning_Python/dice_rolling_simulator.py", line 12, in <module>
dice()
File "C:/Users/a/Documents/Code/Learning_Python/dice_rolling_simulator.py", line 6, in dice
while user[0].lower() == 'y':
IndexError: string index out of range