简体   繁体   中英

How do I subtract the asterisks in a descending order on my code?

I want the marble dots to be displayed in the output, and as the number of marbles go down, so do the asterisks. I don't know what command will have the asterisks descend as the while loop runs.

#!/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("")

Nearly there!

Just change the print from hard-coded 10 to the number of marbles remaining

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

You could also replace it with:

print('*' * marbles)

Which will print * once per marble.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM