[英]LeetCode Reverse Integer (ValueError: invalid literal for int() with base 10: '' )
[英]ValueError: invalid literal for int() with base 10: ''turning entry into integer
我正在猜測 Zelle 圖形中的數字,但我的程序似乎無法正常工作。 我試圖讓文本條目成為一個整數。 如果我所做的還有其他問題,我將不勝感激。
我試過做 int(number) 但這沒有用
from graphics import *
import random
hidden=random.randrange(1,10)
def responseDict():
levels = dict()
levels['high'] = 'woah! you are too high!'
levels['low']='oh no! that is too low'
levels['equal']='yes, this is just right!'
return levels
def circles(): # cute, but nothing original here, not even usage
win = GraphWin("Random Circles",300,300)
for i in range(300):
r = random.randrange(256)
b = random.randrange(256)
g = random.randrange(256)
color = color_rgb(r, g, b)
radius = random.randrange(3, 40)
x = random.randrange(5, 295)
y = random.randrange (5, 295)
circle = Circle(Point(x,y), radius)
circle.setFill(color)
circle.draw(win)
time.sleep(.05)
def textBox(win):
message = Text(Point(250,50),'Please guess a number 1 through 10 then click outside the box')
message.draw(win)
message2=Text(Point(250,100),'You have 4 tries, to guess the number correctly.')
message2.draw(win)
for i in range(9):
textEntry =Entry(Point(233,200),10)
textEntry.draw(win)
win.getMouse()
number=textEntry.getText()
guess=int(number)
print(guess)
levels = responseDict()
while guess != hidden:
if guess < hidden:
response = Text(Point(300,300), (levels['low']))
response.draw(win)
again=Text(Point(400,400), 'guess again')
again.draw(win)
textEntry=Entry(Point(233,200),10)
textEntry.draw(win)
win.getMouse()
number=textEntry.getText()
guess=int(number)
print(guess)
response.undraw()
again.undraw()
win.getMouse()
elif guess > hidden:
response2=Text(Point(350,350),(levels['high']))
response2.draw(win)
again2=Text(Point(400,400), 'guess again')
again2.draw(win)
textEntry2=Entry(Point(233,200),10)
textEntry2.draw(win)
win.getMouse()
number=textEntry.getText()
guess=int(number)
print(guess)
response2.undraw()
again2.undraw()
win.getMouse()
else:
response=Text(Point(300,300),(levels['equal']))
response.draw(win)
win.getMouse()
circles()
win = GraphWin('guess number', 700,700)
win.setBackground('brown')
textBox(win)
exitText = Text(Point(400,400), 'Click anywhere to quit')
exitText.draw(win)
win.getMouse()
win.close()
我希望用戶輸入的內容成為一個整數並且我的游戲可以運行!
如果有人輸入文本而不是數字(即Hello
),則int()
給出錯誤
ValueError: invalid literal for int() with base 10: 'Hello'
你必須使用try/except
來捕捉它
number = textEntry.getText()
try:
guess = int(number)
print(guess)
except Exception as ex:
guess = None
#print(ex)
except
我設置了guess = None
所以稍后我可以為此顯示消息
if guess is None:
# show message
response = Text(Point(300, 300), 'It is not number')
response.draw(win)
如果你不分配值guess
中except
那么你可以得到錯誤,這個變量不存在-它可以在第一個循環發生時,在以前的循環未創建變量。
我的完整代碼(有其他變化):
from graphics import *
import random
hidden = random.randrange(1, 10)
def response_dict():
return {
'high': 'woah! you are too high!',
'low': 'oh no! that is too low',
'equal': 'yes, this is just right!',
'none': 'It is not number',
}
def circles():
win = GraphWin("Random Circles",300,300)
for i in range(300):
r = random.randrange(256)
b = random.randrange(256)
g = random.randrange(256)
color = color_rgb(r, g, b)
radius = random.randrange(3, 40)
x = random.randrange(5, 295)
y = random.randrange(5, 295)
circle = Circle(Point(x, y), radius)
circle.setFill(color)
circle.draw(win)
time.sleep(.05)
def textBox(win):
message = Text(Point(250,50),'Please guess a number 1 through 10 then click outside the box')
message.draw(win)
message2 = Text(Point(250,100),'You have 4 tries, to guess the number correctly.')
message2.draw(win)
# you can get it once
levels = response_dict()
# 4 tries
for i in range(4):
textEntry = Entry(Point(233,200),10)
textEntry.draw(win)
win.getMouse()
# get number
number = textEntry.getText()
try:
guess = int(number)
print(guess)
except Exception as ex:
#print(ex)
guess = None
# hide entry - so user can't put new number
textEntry.undraw()
if guess is None:
# show message
response = Text(Point(300,300), levels['none'])
response.draw(win)
elif guess < hidden:
# show message
response = Text(Point(300,300), levels['low'])
response.draw(win)
elif guess > hidden:
# show message
response = Text(Point(350, 350), levels['high'])
response.draw(win)
else:
response = Text(Point(300, 300), levels['equal'])
response.draw(win)
win.getMouse()
circles()
break # exit loop
again = Text(Point(400,400), 'Guess again, click mouse.')
again.draw(win)
# wait for mouse click
win.getMouse()
# remove messages
response.undraw()
again.undraw()
# --- main ----
win = GraphWin('guess number', 700, 700)
win.setBackground('brown')
textBox(win)
exitText = Text(Point(400, 400), 'Click anywhere to quit')
exitText.draw(win)
win.getMouse()
win.close()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.