简体   繁体   English

ValueError:int()的基数为10的无效文字:''随机数生成器

[英]ValueError: invalid literal for int() with base 10: '' Random Numbers Generator

I'm trying to allow for the button created to randomly generate a value in Tkinter yet i keep getting an error stating 我正在尝试允许创建的按钮在Tkinter中随机生成一个值,但我不断收到错误说明

ValueError: invalid literal for int() with base 10: '' ValueError:基数为10的int()的无效文字:''

from tkinter import *
from random import *
global playedNumber

class Paul_Lottery:
    def __init__(self):
        self.winningNumber = randrange(1,10)
        self.userInterface = Tk()
        self.playedNumber = 0


        Label(self.userInterface,text ='Type in your name').pack()
        Entry(self.userInterface,width =100).pack()

        Label(self.userInterface,text= 'Enter a number from 1-10:').pack()
        Entry(self.userInterface,width = 100).pack()
        self.justinNumber = Entry(self.userInterface,width=100)


        self.RandomButton= Button(self.userInterface,text = 'Play',command = self.CheckNumber).pack()
        self.finalresult = StringVar()
        self.finalresultLabel=Label(self.userInterface, textvariable = self.finalresult).pack()
        self.userInterface.mainloop()

    def CheckNumber(self):
            playedNumber = int(self.justinNumber.get())

            if playedNumber==self.winningNumber:
                self.finalresult.set('you are a winner')
            else:
                self.finalresult.set('the winning number is' + str(self.winningNumber) + 'you are not a winner')




def main():
     paul_lottery = Paul_Lottery()

main()

I know the string must be converted into a float but confused as to how to do this as i had already set the playedNumber to 0 as an integer. 我知道字符串必须转换为浮点数但是如何做到这一点很困惑,因为我已经将playbackNumber设置为0作为整数。

It looks like you are trying to convert a string to an integer, but the string may contain something that is not convertible to an int. 看起来您正在尝试将字符串转换为整数,但字符串可能包含无法转换为int的内容。 You should handle this by catching the ValueError exception. 您应该通过捕获ValueError异常来处理此问题。 For example 例如

try:
   playedNumber = int(self.justinNumber.get())
except ValueError as e:
   # Do something reasonable when this error occurs such as
   # 1. Logging an error to help you debug
   # 2. Resetting the widget to a valid state
   # 3. Give the user some feedback (such as a warning message)
   print('Failed to convert to int {}'.format(e))

You are passing an empty string (ie '' ) to your int() method, which is what your error is telling you. 您正在将一个空字符串(即'' )传递给您的int()方法,这是您的错误告诉您的。 It looks to me like you are either pressing your RandomButton without anything entered in the justinNumber field (thus an empty string), or your CheckNumber() method is getting triggered automatically somehow before you are able to enter a guess in your justinNumber field. 在我看来,你要么在没有任何输入justinNumber字段的情况下按下RandomButton (因此是一个空字符串),要么在你能够在justinNumber字段中输入猜测之前以某种方式自动触发你的CheckNumber()方法。 I think that's where your error is coming from. 我认为这就是你的错误来自哪里。 Note that your CheckNumber() method is still looking in this field for a guess, not checking a random number. 请注意,您的CheckNumber()方法仍然在此字段中查找猜测,而不是检查随机数。

To have the button randomly generate a number, how about you just create a new method, call it random_guess() defined like this: 要让按钮随机生成一个数字,你如何创建一个新方法,称之为random_guess()定义如下:

def random_guess(self):
    guess = np.random.randint(1, 10)
    if guess == self.winningNumber:
        self.finalresult.set('you are a winner')
    else:
        self.finalresult.set('the winning number is' + str(self.winningNumber) + 'you are not a winner')

Then tie that to your random button with: 然后将其与随机按钮绑定:

self.RandomButton = Button(self.userInterface, text='Play',
                           command=self.random_geuss).pack()

And if you want to get fancy, I encourage you to find a way to minimize repeated code before random_guess() and your CheckNumber() on your own! 如果你想获得幻想,我鼓励你找到一种方法,在random_guess()和你自己的CheckNumber()之前尽量减少重复的代码!

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

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