[英]How can i make def function not display twice
When I put 'n' to stop making changes to that stats "good choice" will repeat 2 times and it will go back to charc1Stat().当我输入 'n' 停止对该统计数据进行更改时,“好选择”将重复 2 次,然后返回到 charc1Stat()。 I want it to stop after I put in "n".我希望它在输入“n”后停止。 Please tell me what I'm doing wrong, I'm using IDLE Python 3.7.4.请告诉我我做错了什么,我使用的是 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()``
You're calling the Change1() function both in your main() function as well as in your charc1Stat() function.你调用CHANGE1()函数都在main()函数,以及在你的charc1Stat()函数。 Remove it in either one of those and you will only get the message once:在其中任何一个中删除它,您只会收到一次消息:
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.