繁体   English   中英

在我运行此消息后弹出 ValueError: invalid literal for int() with base 10: ''

[英]after i run this message pop up ValueError: invalid literal for int() with base 10: ''

import random
from time import sleep
from os import system

def end(time):
    while True:
        print(str(time))
        time -= 1
        sleep(1)
        if time == 0:
            sleep(2)
            system("cls")
            print("end")
            sleep(3)
            quit()

def main():
    atmp = 0
    money = 200

    print("gambling game")
    print("every time you dont get right you get -10$")
    sleep(5)
    system("cls")
    while True:
        print("you have " + str(money) + "$")
        print("enter an number from 1 to 10")
        ran = random.randint(1,10)
        user = input(">>> ")
        atmp += 1
        money -= 10
        sleep(2)
        print("the answer number is ")
        sleep(2)
        print(str(ran))
        print("")
        print("you did " + str(atmp) + " atempts")
        print("")
        if int(user) == int(ran):
            sleep(2)
            system("Cls")
            print("nice you win")
            end(5)
        elif money == 0:
            sleep(2)
            system("Cls")
            print("you dont have enough money")
            end(5)


if __name__ == "__main__":
    main()

如果我运行这个有时这个错误消息显示“ValueError:int()的无效文字,基数为10:''”我试图找到它,但我能找到它,所以如果你们帮助我,如果有的话,那就太棒了无论如何要缩短这段代码,这太棒了,提前谢谢

当您尝试将非数字字符串转换为 integer 时会发生该错误。 我可能是由于意外输入了一个数字而导致的。 稍微缩短,没有错误:

import random
from time import sleep
from os import system

def end(time):
    while True:
        print(time)
        time -= 1
        sleep(1)
        if not time:
            sleep(2)
            system("cls")
            print("end")
            sleep(3)
            quit()

def main():
    atmp = 0
    money = 200
    print("gambling game\nevery time you dont get right you get -10$")
    sleep(5)
    system("cls")
    while True:
        print(f"you have {money}$\nenter an number from 1 to 10")
        ran = random.randint(1,10)
        user = input(">>> ")
        atmp += 1
        money -= 10
        sleep(2)
        print("the answer number is ")
        sleep(2)
        print(f"{ran}\n")
        print(f"you did {atmp} atempts\n")
        if int(user) == ran:
            sleep(2)
            system("Cls")
            print("nice you win")
            end(5)
        elif money == 0:
            sleep(2)
            system("Cls")
            print("you dont have enough money")
            end(5)

if __name__ == "__main__":
    main()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM