【问题标题】:Quit while len(int) < 1len(int) < 1 时退出
【发布时间】:2019-02-05 01:26:39
【问题描述】:

我是 python 新手,如果输入为空或小于 One int,我正在尝试进行简单的退出。 我收到一条错误消息 - ValueError: invalid literal for int() with base 10: '', when enter nothing, just a enter on launch.

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() 要求最小长度为 1,并且输入只能是数字字符串。
  • 在尝试将其转换为 int 之前存储输入。然后做你的检查。并且仅在通过您的检查时才转换为 int。

标签: python


【解决方案1】:

我们只看这部分代码:

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")

空的用户输入将被传递给int(),但空字符串不能转换为int!因此引发了错误。

您应该先不要将输入转换为 int,而是将其视为字符串:

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: # convert to int here
        clear()
        ...

【讨论】:

    【解决方案2】:

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

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

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

    【讨论】:

    • 我什么时候做的 if len(Shop_Pasirinkimas)
    • 我什么都不输入就说对不起,但是当我输入1时,它只是显示相同的菜单,什么都没有发生
    【解决方案3】:

    如果用户不输入整数,您将在Shop_Pasirinkimas = int(input(...)) 中遇到异常。除了int 没有len() 所以这也会导致错误len(Shop_Pasirinkimas)。您可以执行以下操作来完成您正在尝试的事情

    while True:
        try:
            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 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
        except ValueError:
            sys.exit("SOrry")
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-09-13
      • 2016-01-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-10-15
      • 2022-01-01
      • 2012-11-19
      相关资源
      最近更新 更多