python中的While循环和范围数
正在为学校编写一个代码,我需要对其进行修改,以便只允许用户输入0到20o的范围。这是到目前为止的代码。每次我尝试放置一个while循环时,它都不起作用,我仍然可以输入任何高于200的值。任何帮助都将不胜感激python中的While循环和范围数,python,while-loop,numbers,range,date-range,Python,While Loop,Numbers,Range,Date Range,正在为学校编写一个代码,我需要对其进行修改,以便只允许用户输入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&q
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?")
def limit_输入(提示):
尽管如此:
值=int(输入(提示))
如果值<200:
返回值
打印(“请将您的值保持在200以下。”)
bar1=限制\输入(“第一个条的高度是多少?”)
bar2=限制输入(“第二个条的高度是多少?”)
bar3=限制输入(“第三个条的高度是多少?”)
bar4=限制输入(“第四个条的高度是多少?”)
留给读者的一个练习是添加一个
try
/除了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)
这里没有试图限制范围的东西。是否需要限制所有输入?如果是这样,那么显而易见的答案是创建一个函数,可能称为
limited\u input
,它接受提示字符串作为参数。它可以执行while循环并一直执行提示,直到值在范围内。您不能在哪里放置while循环,您想在哪里放置while循环?您不需要返回值
而不仅仅是返回
?Duh。我将修复这个愚蠢的错误。对于条形图中的b,索引或会更清晰:
/条形图(b)
。实际上,每次您为范围内的x(len(y))编写:
(基本上就是您编写的),都有更好的方法。您是对的。我正在编辑代码。非常感谢。