a = int(input("Enter mark of BIOLOGY: "))
b = int(input("Enter mark of CHEMISTRY: "))
c = int(input("Enter mark of PHYSICS: "))
sum = a + b + c
x = sum
y = 3
avg = x / y
print("Total marks = ", sum)
print("Average marks = ", avg)
我想限制用户的输入,因此它只接受0到90之间的整数。
答案 0 :(得分:1)
要将用户输入限制为0到90,您需要反复要求用户重新输入数据,直到满足0到90的条件。您可以通过实施urllib2.URLError
循环然后while
来满足条件。这是代码:
break
希望这会有所帮助。
答案 1 :(得分:0)
如果你想限制sum变量,你可以在代码的末尾放一个if语句来检查变量是否在边界内。
public static class SharedResources
{
public static double ButtonFontSize
{
get { return 36; }
}
}
答案 2 :(得分:0)
以下代码将确保用户输入指定范围内的数字:
while True:
a = int(input("enter mark of BIOLOGY = "))
if 0 <= a <= 90:
break
else:
print("Invalid mark. Please try again.")
while True:
b = int(input("enter mark of CHEMISTRY = "))
if 0 <= b <= 90:
break
else:
print("Invalid mark. Please try again.")
while True:
c = int(input("enter mark of PHYSICS = "))
if 0 <= c <= 90:
break
else:
print("Invalid mark. Please try again.")
sum = a + b + c
x = sum
y = 3
avg = x / y
print("total marks = ", sum)
print("average marks = ", avg)
这将确保变量a
,b
和c
介于0-90之间。