gpt4 book ai didi

python - 如何在代码中按降序减去星号?

转载 作者:行者123 更新时间:2023-12-02 01:51:55 24 4
gpt4 key购买 nike

我希望在输出中显示弹珠点,并且随着弹珠数量的减少,星号也会减少。我不知道什么命令会让星号在 while 循环运行时下降。

#!/usr/bin/env python3

marbles = 10 #You start out with 10 marbles
marble_dots = "**********" #Pretend these are ten marbles

while (marbles > 0):
print(marble_dots[:10])

#This prints out how many marbles you have left.
# We have to say str(marbles) because marbles is a number
# and we want to use it in a string (letters and other characters)
print("You have " + str(marbles) + " marbles left.")

if (marbles < 4):
print("Warning: You are running low on marbles!!")

#This is another way of saying "Subtract 1 from the marbles variable"
# It is logically the same as writing "marbles = marbles - 1", just shorter
marbles -= 1

# Make a newline, so there's an empty line before the next time we run this loop
print("")

最佳答案

快到了!

只需将打印内容从硬编码的 10 更改为剩余弹珠的数量

while (marbles > 0):

print(marble_dots[:marbles])

您也可以将其替换为:

print('*' * marbles)

每个弹珠都会打印一次 *。

关于python - 如何在代码中按降序减去星号?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70143712/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com