在控制台中输出:0 1 2 3 4 5
在控制台中输出:2 3 4 5 6 7
在控制台中输出:0 2 4 6
在控制台中输出:4 3 2 1 0
在控制台中输出:-1 -2 -3 -4
count = 0
while count < 6:
print(count)
count += 1
count = 2
while count < 8:
print(count)
count += 1
count = 0
while count < 7:
print(count)
count += 2
count = 4
while count >= 0:
print(count)
count += -1
count = -1
while count >= -4:
print(count)
count -= 1
在控制台中,获取一个开始值,一个结束值。 将中间的数字打印出来。
输入:3 9 输出: 4 5 6 7 8
start = int(input("请输入开始值:"))
stop = int(input("请输入结束值:"))
while start < stop - 1:
start += 1
print(start)
在控制台中,获取一个开始值,一个结束值。 将中间的数字打印出来。
输入:9 3 输出: 8 7 6 5 4
start = int(input("请输入开始值:"))
stop = int(input("请输入结束值:"))
while start > stop + 1:
start -= 1
print(start)
在控制台中,获取一个开始值,一个结束值。 将中间的数字打印出来。
start = int(input("请输入开始值:"))
stop = int(input("请输入结束值:"))
dir = 1 if start < stop else -1
while start != stop - dir:
start += dir
print(start)
一张纸的厚度是0.01毫米,请计算对折多少次,超过珠穆朗玛峰8844.43米。
答案:30次
d = 0.00001
count = 0 #计数器
while d < 8844.43:
d *= 2
count +=1
print("第" + str(count) + "次对折的厚度是:" + str(d))
print(count)
游戏运行产生一个1--100之间的随机数。 让玩家重复猜测,直到猜对为止。
输出:大了、小了、猜对了,总共猜了多少次。
import random
random_number = random.randint(1,100)
count = 0
while True:
count += 1
input_number = int(input("请输入数字:"))
if input_number > random_number:
print("大了")
elif input_number < random_number:
print("小了")
else:
print("猜对了,总共猜了" + str(count) + "次")
break
猜数字游戏2.0:若猜了五次还没才对,则终止游戏。
import random
random_number = random.randint(1,100)
count = 0
while count < 5:
count += 1
input_number = int(input("请输入数字:"))
if input_number > random_number:
print("大了")
elif input_number < random_number:
print("小了")
else:
print("猜对了,总共猜了" + str(count) + "次")
break
else:
print("失败了呜呜")
学了一下午的python,还挺好玩哈哈哈,可是我明天开始就要去学车了,可能会学的少点。期待下一次敲代码啦嘻嘻。