[英]Python int/str checking
我有一個家庭作業,並且我已經完成了最少的作業,但是在進行作業時,我對如何使我的程序變得更好感興趣。 代碼的重點是繪制用戶定義的形狀,並在輸入無效輸入時提供反饋。 我想繼續做下去,所以如果用戶輸入一個str作為高度/寬度或坐標之一,它將給出一個錯誤,但是我無法弄清楚該怎么做。 這是一個入門課程,因此,如果您要牢記,我們只是進入while循環,而對於if / else / etc,我們已經進行了循環。
這是我目前擁有的代碼:
def draw_user_shape(pen):
shape = input("Please enter a shape: ")
while shape != "square" and shape != "rectangle" and shape != "triangle":
shape = input("Invalid shape, please enter either square, rectangle, or triangle: ")
if shape == "square":
h = input("Please enter a height: ")
while h != int or int(h) < 1:
h = input("That is an invalid height, please enter an positive integer: ")
h = int(h)
c = input("Please enter a color: ")
while c != str and str(c) != "red" and str(c) != "green" and str(c) != "blue":
c = input("That is an invalid color, please enter either red, blue, or green: ")
c = str(c)
x = input("Please enter an x-coordinate: ")
while x != int and int(x) == 1:
x = input("That is an invalid x-coordinate, please enter an integer: ")
x = int(x)
y = input("Please enter a y-coordinate: ")
while y != int and int(y) == int:
y = input("That is an invalid y-coordinate, please enter an integer: ")
y = int(y)
pen.fillcolor(c)
pen.up()
pen.goto(x,y)
pen.down()
pen.begin_fill()
pen.goto(x,y+h)
pen.goto(x+h,y+h)
pen.goto(x+h,y)
pen.goto(x,y)
pen.end_fill()
pen.up()
elif shape == "rectangle":
h = input("Please enter a height: ")
while h != int and int(h) < 1:
h = input("That is an invalid height, please enter an positive integer: ")
h = int(h)
w = input("Please enter a width: ")
while w != int and int(w) < 1:
w = input("That is an invalid height, please enter an positive integer: ")
w = int(w)
c = input("Please enter a color: ")
while c != str and str(c) != "red" and str(c) != "green" and str(c) != "blue":
c = input("That is an invalid color, please enter either red, blue, or green: ")
c = str(c)
x = input("Please enter an x-coordinate: ")
while x != int and int(x) == 1:
x = input("That is an invalid x-coordinate, please enter an integer: ")
x = int(x)
y = input("Please enter a y-coordinate: ")
while y != int and int(y) == int:
y = input("That is an invalid y-coordinate, please enter an integer: ")
y = int(y)
pen.fillcolor(c)
pen.up()
pen.goto(x, y)
pen.down()
pen.begin_fill()
pen.goto(x,y+h)
pen.goto(x+w,y+h)
pen.goto(x+w,y)
pen.goto(x,y)
pen.end_fill()
pen.up()
elif shape == "triangle":
h = input("Please enter a height: ")
while h != int and int(h) < 1:
h = input("That is an invalid height, please enter an positive integer: ")
h = int(h)
w = input("Please enter a width: ")
while w != int and int(w) < 1:
w = input("That is an invalid height, please enter an positive integer: ")
w = int(w)
c = input("Please enter a color: ")
while c != str and str(c) != "red" and str(c) != "green" and str(c) != "blue":
c = input("That is an invalid color, please enter either red, blue, or green: ")
c = str(c)
x = input("Please enter an x-coordinate: ")
while x != int and int(x) == 1:
x = input("That is an invalid x-coordinate, please enter an integer: ")
x = int(x)
y = input("Please enter a y-coordinate: ")
while y != int and int(y) == int:
y = input("That is an invalid y-coordinate, please enter an integer: ")
y = int(y)
pen.fillcolor(c)
pen.up()
pen.goto(x,y)
pen.down()
pen.begin_fill()
pen.goto(x+w/2,y+h)
pen.goto(x+w,y)
pen.goto(x,y)
pen.end_fill()
pen.up()
def main():
import turtle
pen = turtle.Turtle()
draw_user_shape(pen)
main()
在這種情況下,您想使用異常處理。 基本上,這個想法是接受輸入並嘗試將其轉換為int
。 如果失敗,則會引發ValueError
。 否則,您將取回轉換后的值。 請記住,輸入將始終以str
形式提供給您,因此您不能簡單地測試它是否為int
。 假設您使用的是Python 3,則可以執行以下操作來不斷詢問,直到輸入正確的值為止:
# keep asking for input until we get the right one
while True:
myInput = input('give me a number: ')
try:
myValue = int(myInput)
# if we reach this point, that means we got our number
break # this will jump out of the loop
except ValueError:
# if we reach this point, that means the input was bad
print('invalid input')
我建議使用
not h.isdigit()
檢查字符串h是否不包含整數。 它不適用於浮點數,因為它真正要檢查的是每個數字是否在0-9范圍內,以及.
將不會(正確)識別為數字。
例如線
while h != int or int(h) < 1:
會成為
while not h.isdigit() or int(h) < 1:
順便說一句,我假設您使用的是Python 3.x,否則您的代碼將無法工作,因為input
在Python 2.x中的工作方式有所不同。 在Python 3.x中,它應始終返回字符串,因此沒有任何理由檢查返回的對象是否為字符串。
>>> isinstance('a', int)
False
>>> isinstance(2, int)
True
使用.isdigit()
或.isalnum()
進行檢查 , .isalnum()
您的偏好
例
str_num= 'aaaa7777' str_num.isalnum() # true str_num.isdigit() # false
例
str_num= 'aaaa 7777' # Note that has spaces str_num.isalnum() # false str_num.isdigit() # false
例
str_num= '7777' str_num.isalnum() # True str_num.isdigit() # True
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.