python 中的 While 循环和范围数字
While loop and ranged numbers in python
正在为学校编写代码,我需要将其修改为仅允许用户输入 0 到 20o 的范围。这是到目前为止的代码。每次我尝试放置一个 while 循环时它都不起作用,我仍然可以输入任何高于 200 的值。任何帮助将不胜感激。
import turtle #allows access to a collection of Python functions that make it possible to draw
Jane = turtle.Turtle() #creates a "turtle" to do our drawing for us; we will call the turtle Jane
# the next four lines get input from the user so we know how tall to make the bars in the graph
bar1 = int(input("What is the height of the first bar?" ))
bar2 = int(input("What is the height of the second bar?" ))
bar3 = int(input("What is the height of the third bar?" ))
bar4 = int(input("What is the height of the fourth bar? "))
#the turtle always starts out facing left; the next line rotates it so it faces up
Jane.left(90)
#next we will create a function that will draw one bar of the bar graph
#the function requires us to pass the height of the bar into it
def bar(height):
#Jane will make a line that is as long as the height that the user inputted
Jane.forward(height)
#now we will turn Jane to the left to make the top of the bar
Jane.left(90)
#now Jane needs to more forward to draw the top of the bar
Jane.forward(20)
#now Jane needs to turn left
Jane.left(90)
#now Jane needs to draw a line that is as long as the height
Jane.forward(height)
#now Jane needs to rotate 180 degrees to be ready for the next bar
Jane.right(180)
#now we will call the function four times--one for each bar. Notice that
#we will go in the order 4-3-2-1, since the bars will be drawn from right to left
bar(bar4)
bar(bar3)
bar(bar2)
bar(bar1)
像这样?
def limit_input(prompt):
while True:
value = int(input(prompt))
if value < 200:
return value
print( "Please keep your value below 200.")
bar1 = limit_input("What is the height of the first bar?")
bar2 = limit_input("What is the height of the second bar?")
bar3 = limit_input("What is the height of the third bar?")
bar4 = limit_input("What is the height of the fourth bar?")
留给 reader 的练习是添加 try
/except ValueError
子句以在用户键入非数字内容时捕获。
请看这段代码。我认为您正在寻找这样的代码。
import turtle
Jane = turtle.Turtle()
bars = []
for i in range(4):
height = 201
while height > 200:
height = int(input(f'What is the height of the {i + 1}. bar? (from 1 to 200))'))
bars.append(height)
Jane.left(90)
def bar(height):
Jane.forward(height)
Jane.left(90)
Jane.forward(20)
Jane.left(90)
Jane.forward(height)
Jane.right(180)
for b in bars:
bar(b)
正在为学校编写代码,我需要将其修改为仅允许用户输入 0 到 20o 的范围。这是到目前为止的代码。每次我尝试放置一个 while 循环时它都不起作用,我仍然可以输入任何高于 200 的值。任何帮助将不胜感激。
import turtle #allows access to a collection of Python functions that make it possible to draw
Jane = turtle.Turtle() #creates a "turtle" to do our drawing for us; we will call the turtle Jane
# the next four lines get input from the user so we know how tall to make the bars in the graph
bar1 = int(input("What is the height of the first bar?" ))
bar2 = int(input("What is the height of the second bar?" ))
bar3 = int(input("What is the height of the third bar?" ))
bar4 = int(input("What is the height of the fourth bar? "))
#the turtle always starts out facing left; the next line rotates it so it faces up
Jane.left(90)
#next we will create a function that will draw one bar of the bar graph
#the function requires us to pass the height of the bar into it
def bar(height):
#Jane will make a line that is as long as the height that the user inputted
Jane.forward(height)
#now we will turn Jane to the left to make the top of the bar
Jane.left(90)
#now Jane needs to more forward to draw the top of the bar
Jane.forward(20)
#now Jane needs to turn left
Jane.left(90)
#now Jane needs to draw a line that is as long as the height
Jane.forward(height)
#now Jane needs to rotate 180 degrees to be ready for the next bar
Jane.right(180)
#now we will call the function four times--one for each bar. Notice that
#we will go in the order 4-3-2-1, since the bars will be drawn from right to left
bar(bar4)
bar(bar3)
bar(bar2)
bar(bar1)
像这样?
def limit_input(prompt):
while True:
value = int(input(prompt))
if value < 200:
return value
print( "Please keep your value below 200.")
bar1 = limit_input("What is the height of the first bar?")
bar2 = limit_input("What is the height of the second bar?")
bar3 = limit_input("What is the height of the third bar?")
bar4 = limit_input("What is the height of the fourth bar?")
留给 reader 的练习是添加 try
/except ValueError
子句以在用户键入非数字内容时捕获。
请看这段代码。我认为您正在寻找这样的代码。
import turtle
Jane = turtle.Turtle()
bars = []
for i in range(4):
height = 201
while height > 200:
height = int(input(f'What is the height of the {i + 1}. bar? (from 1 to 200))'))
bars.append(height)
Jane.left(90)
def bar(height):
Jane.forward(height)
Jane.left(90)
Jane.forward(20)
Jane.left(90)
Jane.forward(height)
Jane.right(180)
for b in bars:
bar(b)