首页 > 解决方案 > len(int) < 1 时退出

问题描述

我是 python 新手,如果输入为空或小于 One int,我正在尝试进行简单的退出。我收到一条错误消息 - ValueError: invalid literal for int() with base 10: '', 当不输入任何内容时,只需在启动时输入。

import sys
import os
import getpass
def clear(): return os.system('clear')
ballance = 500.00
# Garage Stockas
Wood_InStock = 600
Weed_InStock = 300
Gun_InStock = 15
Gun_Ammo_InStock = 500 * 30 # X30 Total 15000
# Kainos
Gun_Ammo_Price = 15.50
Wood_Price = 3.50
Weed_Price = 9.50
Gun_Price = 250.50
# Produktai
medis = '~ Elemental Wood ~'
weed = '~ Indoor Kush ~'
gun = '~ Shotgun  ~'
gun_ammo = '~ Shotgun ammo 30x ~'
# Inventory
Wood_Inventory = 0
Weed_Inventory = 0
Gun_Inventory = 0
Gun_Ammo_Inventory = 0
# No Money
Not_Enough_Money = '~ Sorry you dont have enough money'


while True:
    Shop_Pasirinkimas = int(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 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)))
        # Price per wood - 3.50
        ballance -= ( Wood_Price * WoodPirkimo_Skaic)
        Wood_Inventory += WoodPirkimo_Skaic
        Wood_InStock -= WoodPirkimo_Skaic
        print("~ In stock of {}, left {}".format(medis,Wood_InStock))
        print("~ Successfully bought {}, Your Ballance is {}\n".format(medis,ballance))
        print('Inventory:')
        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:
            continue
        elif "No" in Buymore or "no" in Buymore:
            break
        else:
            break

标签: python

解决方案


int(x,base) 函数将从任何数字或字符串返回整数对象。基数默认为 10。如果 x 是字符串,则其各自的数字应在相对于该基数的可能值范围内。

因为,没有输入任何内容,它被认为是无效的文字。

因此,请使用输入作为可以轻松解决问题的字符串。


推荐阅读