我已经定义了一个名为Start()
的函数,我已经完成了这个函数,我想再次使用它,它将无法正常工作。它第一次工作,但如果我再次尝试使用此功能,它将无法正常工作。对不起,如果这个问题不明确。
import random
dice_numb = ["1", "2", "3", "4", "5", "6"]
def Start():
print("Hello Welcome To Dice Rolling")
roll = input("\nWould You Like To Roll? Y/N: ")
while roll != "Y":
if roll == "Y":
print("Alright")
randomindex = random.randint(0,len(dice_numb)-1)
print (dice_numb[randomindex])
break
if roll == "N":
print("Okay,Cya")
sys.exit()
again = input("Would You Like To Roll Again? Y/N: ")
while again != "Y":
if again == "Y":
print("Alright,Lets Do It!")
if again -- "N":
print("Okay,Cya.")
sys.exit()
Start()
答案 0 :(得分:1)
欢迎来到Python的世界:)。我在您的代码中看到了许多问题:
将这些更改与您的代码进行比较并阅读评论:
import random
def Start():
print("Hello Welcome To Dice Rolling")
roll = input("\nWould You Like To Roll? Y/N: ")
while roll == "Y":
print("Alright")
dice_no = random.randint(0, 6) # or random.randint(0, len(dice_numb) -1)
print(dice_no) # No need to index list
roll = input("\nWould You Like To Roll Again? Y/N: ")
else:
print("Okay,Cya")
Start()
注意:不要使用大写字母启动函数名称,这种命名约定通常用于类而不是函数。
答案 1 :(得分:0)
我不知道这是否有效;我也是python的新手。我想你忘了导入“sys”模块...
答案 2 :(得分:0)
试试这样:
import random
import sys
dice_numb = ["1", "2", "3", "4", "5", "6"]
def Start():
print("Hello Welcome To Dice Rolling")
roll = input("\nWould You Like To Roll? Y/N: ")
if roll.upper() == "Y":
print("Alright")
print random.choice(dice_numb)
if roll.upper() == "N":
print("Okay,Cya")
sys.exit()
while 1:
again = input("Would You Like To Roll Again? Y/N: ")
if again.upper() == "Y":
print("Alright,Lets Do It!")
if again.upper() == "N":
print("Okay,Cya.")
break
Start()