简体   繁体   中英

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

if i run this some time this error message show "ValueError: invalid literal for int() with base 10: ''" i tried to find it but i can find it so it would be awesome if you guys help me and if there is anyways to shorten this code that would be awesome too great thanks in advance

That error occurs when you try to convert a non-digit string into an integer. I might have been caused by accidentally entering blank for a number. Slightly shortened with no error:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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