簡體   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