代码对齐,如果固定功能,并尝试添加ValueError异常

问题描述 投票:-2回答:1

最近,我开始蟒蛇,并开始制定通过CMD迷你店。这里是我的代码,我试图添加除错误,但我的对齐不好,任何想法我应该可以解决吗?或固定的。什么即时试图在这里做的就是添加ValueError异常,所有Shop_Pasirinkimas = 1,2,3。你也可以看看到所有的如果,否则ELIF功能,并检查他们是很好的。对于这样做,我真的给你多少尊重。对不起,我的英语不好。 P.S我是新来的Python。

while True:
    Shop_Pasirinkimas = input("~ What would you like to buy?\n1. {medis}\n2. {weed}\n3. {gun}\n".format(medis=medis,weed=weed,gun=gun))
    if len(Shop_Pasirinkimas) < 1:
        sys.exit("SOrry")
    elif int(Shop_Pasirinkimas) == 1:
        clear()
        WoodPirkimo_Skaic = int(input("How much {medis} would you like to buy? ".format(medis=medis) + "Wood Now in Stock - {woodins}\n".format(woodins=Wood_InStock)))
        BendraKaina = (WoodPirkimo_Skaic * Wood_Price)
        if ballance < BendraKaina or Wood_InStock < WoodPirkimo_Skaic:
            clear()
            print(Not_Enough_Money + ". Your Ballance: {} Eur\n".format(ballance))
            continue
        else:
            # Price per wood - 3.50
            ballance -= ( Wood_Price * WoodPirkimo_Skaic)
            Wood_Inventory += WoodPirkimo_Skaic
            Wood_InStock -= WoodPirkimo_Skaic
            clear()
            print("~ In stock of {}, left {}".format(medis,Wood_InStock))
            #print("- {} Eur * Successfully bought {} {} for {} Eur.".format(weed,weed,))
            print('{} Inventory:'.format(Vardas))
            print("~ You have {} {}, {} ammo\n".format(Gun_Inventory,gun,Gun_Ammo_Inventory))
            print("~ You have {}, of {}\n".format(Weed_Inventory,weed))
            print("~ You have {}, of {}\n".format(Wood_Inventory,medis))
            Buymore = input("Would you like to buy anything more?... Yes/No\n")
            if "Yes" in Buymore or "yes" in Buymore or "Y" in Buymore or "y" in Buymore:
                clear()
                continue
            elif "No" in Buymore or "no" in Buymore or "n" in Buymore or "n" in Buymore:
                break
            else:
                break

    elif int(Shop_Pasirinkimas) == 2:
            clear()
            WeedPirkimo_Skaic = int(input("How much {weed} would you like to buy?".format(weed=weed) + " Now in Stock - {weedins}\n".format(weedins=Weed_InStock)))
            BendraKaina = (WeedPirkimo_Skaic * Weed_Price)
            if ballance < BendraKaina:
                clear()
                print(Not_Enough_Money + ". Your Ballance: {} Eur\n".format(ballance))
                continue
            elif Weed_InStock < WeedPirkimo_Skaic:
                clear()
                print(Not_Enough_Money + ". Your Ballance: {} Eur\n".format(ballance))
                continue
                    # Price per Gram - 9.50
                ballance -= (Weed_Price * WeedPirkimo_Skaic)
                Weed_Inventory += WeedPirkimo_Skaic
                Weed_InStock -= WeedPirkimo_Skaic
                clear()
                print("~ In stock of {}, left {}".format(weed,Weed_InStock))
                print("~ Successfully bought {}, Your Ballance is {}\n".format(weed,ballance))
                print('Inventory:')
                print("~ You have {} {}, {} ammo\n".format(Gun_Inventory,gun,Gun_Ammo_Inventory))
                print("~ You have {}, of {}\n".format(Weed_Inventory,weed))
                print("~ You have {}, of {}\n".format(Wood_Inventory,medis))
                Buymore = input("Would you like to buy anything more?... Yes/No\n")
                if "Yes" in Buymore or "yes" in Buymore or "Y" in Buymore or "y" in Buymore:
                    clear()
                    continue
                elif "No" in Buymore or "no" in Buymore or "N" in Buymore or "n" in Buymore:
                    break
                else:
                    break

# NO MONEY, NOT IN STOCK - WORKING
    elif int(Shop_Pasirinkimas) == 3:
            clear()
            GunPirkimo_Skaic = int(input("How much {gun} would you like to buy? ".format(gun=gun) + "\nShotun Now in Stock - {gunins}\n".format(gunins=Gun_InStock)))
            BendraKaina = (GunPirkimo_Skaic * Gun_Price)
    if ballance < BendraKaina:
            clear()
            print(Not_Enough_Money + ". Your Ballance: {} Eur\n".format(ballance))
            continue
            # Nebeliko Storage.
    elif Gun_Ammo_InStock < (GunPirkimo_Skaic * 30):
        clear()
        print("* No more left in stock : \n {} Left - {}\n {} Left - {}\n".format(gun,Gun_InStock,gun_ammo,Gun_Ammo_InStock))
    else:
        ballance -= (Gun_Price * GunPirkimo_Skaic)
        Gun_Inventory += GunPirkimo_Skaic
        Gun_InStock -= GunPirkimo_Skaic
        Gun_Ammo_InStock -= (GunPirkimo_Skaic * 30)
        Gun_Ammo_Inventory += (GunPirkimo_Skaic * 30)
        clear()
        print("~ In stock of {}, left {}".format(gun,Gun_InStock))
        print("~ Successfully bought {}, Your Ballance is {}\n".format(gun,ballance))
        print('Inventory:')
        print("~ You have {} {}, {} ammo\n".format(Gun_Inventory,gun,Gun_Ammo_Inventory))
        print("~ You have {}, of {}\n".format(Weed_Inventory,weed))
        print("~ You have {}, of {}\n".format(Wood_Inventory,medis))
        if Gun_Ammo_Inventory <= 30:
            BuyMoreAmmo = input("~ You have only {}, Maybe you would like to buy any More gun Ammo?... Yes/No\n".format(Gun_Ammo_Inventory))
            if "Yes" in BuyMoreAmmo or "yes" in BuyMoreAmmo or "Y" in BuyMoreAmmo or "y" in BuyMoreAmmo:
                clear()
                AmmoPirkimo_Skaic = int(input("~ 30x Ammo for {}, costs. How much Ammo Packets you would like to buy?".format(gun,Gun_Ammo_Price)))
                if Gun_Ammo_InStock < BendraKaina:
                    clear()
                    print(Not_Enough_Money + ". Your Ballance: {} Eur\n".format(ballance))
                    continue
                ballance -= (AmmoPirkimo_Skaic * Gun_Ammo_Price)
                Gun_Ammo_InStock -= (AmmoPirkimo_Skaic * 30)
                Gun_Ammo_Inventory += (AmmoPirkimo_Skaic * 30)
                print("You have {} bullets".format(Gun_Ammo_Inventory))
                print("Ammo Left in stock {} bullets".format(Gun_Ammo_InStock))
            elif "No" in BuyMoreAmmo or "no" in BuyMoreAmmo or "N" in BuyMoreAmmo or "n" in BuyMoreAmmo:
                sys.exit("Byee")
            else:
                sys.exit("Byeee!")
        else:
                Buymore = input("Would you like to buy anything more?... Yes/No\n")
                if " " in Buymore or len(Buymore) < 1:
                    print("SOrry")
                    continue
                elif "Yes" in Buymore or "yes" in Buymore or "Y" in Buymore or "y" in Buymore:
                    clear()
                    continue
                elif "No" in Buymore or "no" in Buymore or "N" in Buymore or "n" in Buymore:
                    break
                else:
                    break











"""
while True:
    UserName = input("What Username you want?")
    if len(UserName) <= 1:
        print("Bad")
    else:
        ballance -= 500
        print("Account created {}, Ballance = {}".format(UserName,ballance))
python
1个回答
0
投票

你不需要try/except。不要使用int(Shop_Pasirinkimas),只是比较输入字符串:

while True:
    if Shop_Pasirinkimas == "1":
        ...
    elif Shop_Pasirinkimas == "2":
        ...
    elif Shop_Pasirinkimas == "3":
        ...
    else:
        print("Enter 1, 2, or 3")
        continue

    # rest of loop code here
© www.soinside.com 2019 - 2024. All rights reserved.