python - 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
解决方案
int(x,base) 函数将从任何数字或字符串返回整数对象。基数默认为 10。如果 x 是字符串,则其各自的数字应在相对于该基数的可能值范围内。
因为,没有输入任何内容,它被认为是无效的文字。
因此,请使用输入作为可以轻松解决问题的字符串。
推荐阅读
- c++ - 用于迭代多个集合的数据结构
- javascript - 动态设置全局 JS 变量
- javascript - 访问 Map 时超出 Javascript 最大调用堆栈大小错误
- php - 如何在考试中获得学生排名
- php - 如何:将 Laravel 集合折叠成逗号分隔的字符串?
- swift - 使用 swift 调用 webAPI
- android - 反应本机文本输入比较。不适用于 Android 中的特定位置
- python - 我应该在哪里放置单元测试的辅助函数?
- c++ - 我在 C++ 中收到的每个错误都有 4 个相同的无用错误
- kotlin - 具有可变数量的类型化参数和具有相同数量类型化参数的闭包的函数?