首页 > 解决方案 > 我的 for 循环工作有问题

问题描述

我的 For 循环似乎没有“激活”或工作。我正在编写一个关于俄勒冈小道的程序,并且正在研究“商店”部分。这是它一直在做的事情,

首先它是在问同样的问题“你想买多少食物”,直到我按下 E 打破它。现在它不做数学运算或激活 for 循环。这是我正在使用的代码:

    import random
global all
global PlayerPerk

#Organ Trail Start--
print("You may:")
print("1. Travel the road")
print("2. Learn More about the road")
choiceStart = input("What is your choice?  ")




    def Shop(PlayerPerk, money):
        global all
        #intro
        print("Before You set off on your Journey, you will need some supplies to survive the dangerous trail. You will need Food, Ammo, Spare Parts, Clothing, and Oxen")
        #money define
        if PlayerPerk == "1":
            print("You are A cop. You have $400 to spend.")
            money = 400
        elif PlayerPerk == "2":
            print("You are a Clerk. You have $700 to spend.")
            money = 700
        elif PlayerPerk == "3":
            print("You are a Banker. You have $1600 to spend.")
            money =1600

        print("Welcome to My shop! First thing, how much Food would you like to buy?")
        #Food -------------------------------------------------------------------------------
        FoodToBuy = input("How Much food would you like (each cost $2):")
        MoneySubtract = FoodToBuy*2
        for i in range(1,1000):
            if FoodToBuy == int:
                if money - MoneySubtract > 0:
                    money -= MoneySubtract
                    print("You spent: ",MoneySubtract, "You now have: ",money)
                    Food = FoodToBuy
                    MoneySubtract = 0
                else:
                    print("Insufficent Funds!")
                    Shop(PlayerPerk,money)
            elif FoodToBuy == "e":
                break


        #Ammo --------------------------------------------------------------------------------

        print("Now lets buy some Ammo!")
        AmmoToBuy = input("How Much Rifle Ammo would you like to buy (each box cost $5):")
        MoneySubtract = AmmoToBuy*5
        for i in range(1,1000):
            if AmmoToBuy == int:
                if money - MoneySubtract > 0:
                    money -= MoneySubtract
                    print("You spent:",MoneySubtract, "You now have: ",money)
                    Ammo = AmmoToBuy
                    MoneySubtract = 0
                else: 
                    print("Insufficent Funds!")
                    Shop(PlayerPerk,money)
            elif AmmoToBuy=="e":
                break
        #Spare Parts---------------------------------------------------------------------------

        print("Now lets buy some Spare Parts!")
        SparePartsToBuy = input("How much Spare Parts would you like to buy (each costs $20 [max of 9]):")
        MoneySubtract = SparePartsToBuy*20
        for i in range(1,1000):
            if SparePartsToBuy == int:
                if money - MoneySubtract > 0:
                    money -= MoneySubtract
                    print("You spent:", MoneySubtract, "You now have: ",money)
                    SpareParts = SparePartsToBuy
                    MoneySubtract = 0
                else:
                    print("Insufficent Funds!")
                    Shop(PlayerPerk,money)
            elif AmmoToBuy=="e":
                break
            else:
                print("Unknown input!")
                Shop(PlayerPerk, 0)
        #chlothing-----------------------------------------------------------------------------

        print("Now, you need to buy some chlothes!")
        ClothesToBuy = input("How many Clothes would you like to buy (each pair costs $5):")
        MoneySubtract = ClothesToBuy*5
        for i in range(1,1000):
            if ClothesToBuy == int:
                if money - MoneySubtract > 0:
                    money -= MoneySubtract
                    print("You spent:", MoneySubtract, "You now have: ",money)
                    SpareParts = ClothesToBuy
                    MoneySubtract = 0
                else:
                    print("Insufficent Funds!")
                    Shop(PlayerPerk,money)
            elif AmmoToBuy=="e":
                break
            else:
                print("Unknown input!")
                Shop(PlayerPerk, 0)

        #oxen-------------------------------------------------------------------------------------

        print("Finally, you need to buy some Ox!")
        OxenToBuy = input("How many Oxen would you like to buy (each costs $30):")
        MoneySubtract = OxenToBuy*30
        for i in range(1,1000):
            if OxenToBuy == int:
                if money - MoneySubtract > 0:
                    money -= MoneySubtract
                    print("You spent:", MoneySubtract, "You now have: ",money)
                    SpareParts = OxenToBuy
                    MoneySubtract = 0
                else:
                    print("Insufficent Funds!")
                    Shop(PlayerPerk,money)
            if AmmoToBuy=="e":
                break
            else:
                print("Unknown input!")
                Shop(PlayerPerk, 0)


    #----------------------------------------------------------------------------------------------

    def MonthStart(PlayerPerk):
        global all
        print("It is 1848. Your jumping off place for oregon is Independence, Missouri. You must decide which month to leave Indepence.")
        print("1. March")
        print("2. April")
        print("3. May")
        print("4. June")
        print("5. July")
        print("6. ask for advice")
        StartingMonth = input("What is your choice?  ")

        if StartingMonth == ("6"):
            global all
            print("You attend a public meeting held for 'folks with the California - Oregon fever.' You're told:")
            print("If you leave too early, there won't be any grass for your oxen to eat.")
            print("if you leave to late, you may not get to Oregon before winter comes.")
            print("If you leave at just the write time, there will be green grass and the weather will still be cool.")
            exMonthChoice = input("Would you like to stop viewing?(y/n)  ")
            if exMonthChoice ==("Y" or "y"):
                MonthStart(PlayerPerk)
            elif exMonthChoice == ("N"or"n"):
                print("Still viewing")
            else:
                print("Unknown usage.")

        elif StartingMonth == ("1"):
            Month = "March"
            print("You chose: ", Month )
            Shop(PlayerPerk,0)
        elif StartingMonth == ("2"):
            Month = "April"
            print("You chose: ", Month )
            Shop(PlayerPerk,0)
        elif StartingMonth == ("3"):
            Month = "May"
            print("You chose: ", Month )
            Shop(PlayerPerk,0)
        elif StartingMonth == ("4"):
            Month = "June"
            print("You chose: ", Month)
            Shop(PlayerPerk,0)
        elif StartingMonth == ("5"):
            Month = "July"
            print("You chose: ", Month)
            Shop(PlayerPerk,0)

        global all
            ############################

    #NAMING
    def NAMING(PlayerPerk):
        global all
        print("Now lets name your person")
        Name1 = input("What is the name of the Wagon leader  ")
        Name2 = input("What is the name of the 2nd person in the family?  ")
        Name3 = input("What is the name of the 3rd person in the family?  ")
        Name4 = input("What is the name of the 4th person in the family?  ")
        Name5 = input("What is the name of the 5th person in the family?  ")
        NameVerification = input("Are these names correct?")
        if NameVerification == ("Y" or "y"):
            MonthStart(PlayerPerk)
        elif NameVerification ==("N" or "n"):
            NAMING(PlayerPerk)

    #Start OCCUPATION
    def Occupation():
        global all
        ############################################################
        print("Many people find themselves on the road. You may : ")
        print("1. Be a cop from Kentucky.")
        print("2. Be a clerk from Jersey City.")
        print("3. Be a Banker from Boston.")
        print("4. Find out the differences between these choices.")
        ############################################################
        jobChoice = input("What is your choice?  ")

        if jobChoice == ("4"):
            print("Traveling to Oregon isn't easy! But if you are a banker, you'll have more money for supplies and services than a cop or a clerk.")
            print(" But, a cop has a higher chance of haveing a successful hunt!")
            print("And a Clerk has a higher survival rate!")
            exChoice1 = input("Would you like to stop viewing your options?(y/n)  ")
            if exChoice1 == ("y" or "Y"):
                Occupation()
        ###############################
        elif jobChoice ==("1"):
            print("You chose to be a cop from Kentucky!")
            PlayerPerk = ("1")
            NAMING(PlayerPerk)

        elif jobChoice == ("2"):
            print("You chose to be a clerk from Jersey City!")
            PlayerPerk = ("2")
            NAMING(PlayerPerk)

        elif jobChoice == ("3"):
            print("You chose to be a Banker from Boston!")
            PlayerPerk = ("3")
            NAMING(PlayerPerk)



    #Call choices
    if choiceStart == ("1"):
        Occupation()

这是我遇到麻烦的特定代码:

def Shop(PlayerPerk, money):
global all
#intro
print("Before You set off on your Journey, you will need some supplies to survive the dangerous trail. You will need Food, Ammo, Spare Parts, Clothing, and Oxen")
#money define
if PlayerPerk == "1":
    print("You are A cop. You have $400 to spend.")
    money = 400
elif PlayerPerk == "2":
    print("You are a Clerk. You have $700 to spend.")
    money = 700
elif PlayerPerk == "3":
    print("You are a Banker. You have $1600 to spend.")
    money =1600

print("Welcome to My shop! First thing, how much Food would you like to buy?")
#Food -------------------------------------------------------------------------------
FoodToBuy = input("How Much food would you like (each cost $2):")
MoneySubtract = FoodToBuy*2
for i in range(1,1000):
    if FoodToBuy == int:
        if money - MoneySubtract > 0:
            money -= MoneySubtract
            print("You spent: ",MoneySubtract, "You now have: ",money)
            Food = FoodToBuy
            MoneySubtract = 0
        else:
            print("Insufficent Funds!")
            Shop(PlayerPerk,money)
    elif FoodToBuy == "e":
        break


#Ammo --------------------------------------------------------------------------------

print("Now lets buy some Ammo!")
AmmoToBuy = input("How Much Rifle Ammo would you like to buy (each box cost $5):")
MoneySubtract = AmmoToBuy*5
for i in range(1,1000):
    if AmmoToBuy == int:
        if money - MoneySubtract > 0:
            money -= MoneySubtract
            print("You spent:",MoneySubtract, "You now have: ",money)
            Ammo = AmmoToBuy
            MoneySubtract = 0
        else: 
            print("Insufficent Funds!")
            Shop(PlayerPerk,money)
    elif AmmoToBuy=="e":
        break
#Spare Parts---------------------------------------------------------------------------

print("Now lets buy some Spare Parts!")
SparePartsToBuy = input("How much Spare Parts would you like to buy (each costs $20 [max of 9]):")
MoneySubtract = SparePartsToBuy*20
for i in range(1,1000):
    if SparePartsToBuy == int:
        if money - MoneySubtract > 0:
            money -= MoneySubtract
            print("You spent:", MoneySubtract, "You now have: ",money)
            SpareParts = SparePartsToBuy
            MoneySubtract = 0
        else:
            print("Insufficent Funds!")
            Shop(PlayerPerk,money)
    elif AmmoToBuy=="e":
        break
    else:
        print("Unknown input!")
        Shop(PlayerPerk, 0)
#chlothing-----------------------------------------------------------------------------

print("Now, you need to buy some chlothes!")
ClothesToBuy = input("How many Clothes would you like to buy (each pair costs $5):")
MoneySubtract = ClothesToBuy*5
for i in range(1,1000):
    if ClothesToBuy == int:
        if money - MoneySubtract > 0:
            money -= MoneySubtract
            print("You spent:", MoneySubtract, "You now have: ",money)
            SpareParts = ClothesToBuy
            MoneySubtract = 0
        else:
            print("Insufficent Funds!")
            Shop(PlayerPerk,money)
    elif AmmoToBuy=="e":
        break
    else:
        print("Unknown input!")
        Shop(PlayerPerk, 0)

#oxen-------------------------------------------------------------------------------------

print("Finally, you need to buy some Ox!")
OxenToBuy = input("How many Oxen would you like to buy (each costs $30):")
MoneySubtract = OxenToBuy*30
for i in range(1,1000):
    if OxenToBuy == int:
        if money - MoneySubtract > 0:
            money -= MoneySubtract
            print("You spent:", MoneySubtract, "You now have: ",money)
            SpareParts = OxenToBuy
            MoneySubtract = 0
        else:
            print("Insufficent Funds!")
            Shop(PlayerPerk,money)
    if AmmoToBuy=="e":
        break
    else:
        print("Unknown input!")
        Shop(PlayerPerk, 0)

我遇到的唯一问题是使 for 循环工作。我计划在代码开发时使用所有变量。如果您知道出了什么问题或有任何有关任何信息的信息,请告诉我!先感谢您!

标签: pythonpython-3.x

解决方案


推荐阅读