练习
-
判断下列逻辑语句的布尔值
- 1 > 1 or 3 < 4 or 4 > 5 and 2 > 1 and 9 > 8 or 7 < 6 答案: True
- not 2 > 1 and 3 < 4 or 4 > 5 and 2 > 1 and 9 > 8 or 7 < 6 答案: False
-
求出下列逻辑语句的值
- 8 or 3 and 4 or 2 and 0 or 9 and 7 答案: 8
- 0 or 2 and 3 and 4 or 6 and 0 or 3 答案: 4
-
写出下列逻辑语句的值【答案中的括号代表最终结果取决的那个值】
- 6 or 2 > 1 答案:True(6)
- 3 or 2 > 1 答案:True(3)
- 0 or 5 < 4 答案:False(0)
- 5 < 4 or 3 答案:True(3)
- 2 > 1 or 6 答案:True(2>1)
- 3 and 2 > 1 答案:True(2>1)
- 0 and 3 > 1 答案:False(0)
- 2 > 1 and 3 答案:True(2>1)
- 3 > 1 and 0 答案:False(0)
-
猜数字,设定一个数字,让用户输入数字与之比较,如果输入大了,就显示“猜测的结果大了”。如果输入小了,就显示”猜测结果小了“。只要正确猜对数字以后,才可以退出循环
num = 44 while True: user_num = input("请输入数字:") if int(user_num) > 44: print("大了") elif int(user_num) < 44: print("小了") else: print("恭喜你猜对了") break
-
在上一题的基础上,添加一个设置(只能猜三次),如果在三次内猜对了,退出循环,如果三次之内没猜对,也退出循环,并输出”太笨了“。
num = 44
a = 1
while a <= 3:
user_num = input("请输入数字:")
if int(user_num) > 44:
print("大了")
a += 1
elif int(user_num) < 44:
print("小了")
a += 1
else:
print("恭喜你猜对了")
break
if int(a) == 4:
print("太笨了")
- 在第五题的基础上,添加设置:允许用户尝试三次,如果都没猜对,就问用户是否还想继续尝试?,如果用户输入(Y),就继续让用户尝试三次,以此往复,直到猜对为止。如果用户输入(N),就退出程序
num = 44
a = 1
while a <= 3:
user_num = input("请输入数字:")
if int(user_num) > 44:
print("大了")
a += 1
elif int(user_num) < 44:
print("小了")
a += 1
else:
print("恭喜你猜对了")
break
if int(a) == 4:
v1=input("请输入Y or N :")
if v1 == "Y":
a = 1
continue
elif v1 == "N":
break
- 使用两种方式,输出1-10,并不显示7
a = 0
while a < 10:
a += 1
if a == 7:
continue
print(a)
- 求1-100的所有数的和
a = 0
i = 1
while i <= 100:
a += i
i += 1
print(a)
- 输出1-100内所有的奇数
a = 1
while a < 100:
print(a)
a += 2
- 输出1-100内所有的偶数
a = 2
while a < 100:
print(a)
a += 2
- 求1-2+3-4+5...+99的结果
a = 0
c = 0
i = 1
b = 2
while i < 100:
a += i
i += 2
c += b
b += 2
print(a-c)
- 用户登录(三次输错机会)且每次输错后显示剩余次数(提示:使用字符串格式化)
a = 1
while a <= 3:
b = input("username:")
c = input("password:")
if a == "liye" and b == 123456 :
print("Login successful")
else:
print("Login failed")
d = 3-a
print("剩余次数{}".format(d))
a += 1
标签:练习题,python,44,while,num,答案,print,True
From: https://www.cnblogs.com/liye123/p/18520478