[英]How can i make def function not display twice
當我輸入 'n' 停止對該統計數據進行更改時,“好選擇”將重復 2 次,然后返回到 charc1Stat()。 我希望它在輸入“n”后停止。 請告訴我我做錯了什么,我使用的是 IDLE Python 3.7.4。
P1=input("Enter your name player 1: ")
def main():
stats1,choice1=charc1Stat()
Change1(choice1,stats1)
def charc1Stat():
totalPoints=15
defsPoints1=0
atckPoints1=0
while totalPoints>0:
charc1= input("{}, choose defs and atck to add points to ( you have a total of {} left): ".format(P1, totalPoints))
charc1=charc1.lower()
charc1=charc1.rstrip()
charc1=charc1.lstrip()
if charc1 == "defs":
d1 = eval(input("How many points do you want to add to defs?: "))
defsPoints1+= d1
totalPoints= totalPoints - d1
if totalPoints<0:
print("Enter a number that is",totalPoints,"or less")
elif charc1 == "atck":
a1 = eval(input("How many points do you want to add to atck?: "))
atckPoints1 += a1
totalPoints= totalPoints - a1
if totalPoints<0:
print("Enter a number that is",totalPoints,"or less")
else:
print("Enter atck or defs to add points")
stats1={"name":P1, "defense":defsPoints1, "attack":atckPoints1}
print("{} has {} points in defs and {} points in atck.".format(stats1["name"], stats1["defense"],stats1['attack']))
choice1= input("Do you wish to change this? [y/n]: ")
choice1=choice1.lower()
choice1=choice1.rstrip()
choice1=choice1.lstrip()
Change1(choice1,stats1)
return stats1,choice1
def Change1(choice1,stats1):
if choice1[0]=='y':
stats1,choice1=charc1Stat()
if choice1[0]=='n'
print("good choice")
main()``
你調用CHANGE1()函數都在main()函數,以及在你的charc1Stat()函數。 在其中任何一個中刪除它,您只會收到一次消息:
P1=input("Enter your name player 1: ")
def main():
stats1,choice1=charc1Stat()
def charc1Stat():
totalPoints=15
defsPoints1=0
atckPoints1=0
while totalPoints>0:
charc1= input("{}, choose defs and atck to add points to ( you have a total of {} left): ".format(P1, totalPoints))
charc1=charc1.lower()
charc1=charc1.rstrip()
charc1=charc1.lstrip()
if charc1 == "defs":
d1 = eval(input("How many points do you want to add to defs?: "))
defsPoints1+= d1
totalPoints= totalPoints - d1
if totalPoints<0:
print("Enter a number that is",totalPoints,"or less")
elif charc1 == "atck":
a1 = eval(input("How many points do you want to add to atck?: "))
atckPoints1 += a1
totalPoints= totalPoints - a1
if totalPoints<0:
print("Enter a number that is",totalPoints,"or less")
else:
print("Enter atck or defs to add points")
stats1={"name":P1, "defense":defsPoints1, "attack":atckPoints1}
print("{} has {} points in defs and {} points in atck.".format(stats1["name"], stats1["defense"],stats1['attack']))
choice1= input("Do you wish to change this? [y/n]: ")
choice1=choice1.lower()
choice1=choice1.rstrip()
choice1=choice1.lstrip()
Change1(choice1,stats1)
return stats1,choice1
def Change1(choice1,stats1):
if choice1[0]=='y':
stats1,choice1=charc1Stat()
if choice1[0]=='n':
print("good choice")
main()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.