简体   繁体   中英

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(). I want it to stop after I put in "n". Please tell me what I'm doing wrong, I'm using 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. 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()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM