简体   繁体   中英

Python: How can I make my elif loop work?

I just started making a game like thing, and for some reason, the elif loop isn't doing anything when "upgrade" is entered.

choclate = 0
multiplier = 1
multipliercost = 10
x = 1
while x == 1:
  if input() == (("choclate") + str(choclate+multiplier)):
    choclate = choclate+multiplier
    print("\nYou now have " + str(choclate) + " choclate.\nMultiplier Upgrade Cost: " + str(multipliercost) + " choclate\n")
  elif input() == "upgrade":
    multiplier = multiplier*2
    choclate = choclate-multipliercost
    multipliercost = multipliercost*2.5
    print("You have upgraded your multiplier to " + str(multiplier))

I am very new to coding, so I don't really know what to call this problem.

If you call input() twice, then the user needs to key in twice in each round. if you expect the user to key in only once, then you also need to call input() once in each round, and store it into a variable.

Here is the fix.

choclate = 0
multiplier = 1
multipliercost = 10
x = 1
while x == 1:
    # save the input into variable
    key_in = input()
    print(("choclate") + str(choclate+multiplier))
    if key_in == (("choclate") + str(choclate+multiplier)):
        choclate = choclate+multiplier
        print("\nYou now have " + str(choclate) + " choclate.\nMultiplier Upgrade Cost: " + str(multipliercost) + " choclate\n")
    elif key_in == "upgrade":
        multiplier = multiplier*2
        choclate = choclate-multipliercost
        multipliercost = multipliercost*2.5
        print("You have upgraded your multiplier to " + str(multiplier))

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