Python Forum
how do I make a number only print once but the number keeps on decreasing?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
how do I make a number only print once but the number keeps on decreasing?
#1
import time
import winsound

# User Input
plan = input("What Your Plan For Today? : ").lower()
times = int(input("What Number? : "))
choose = input("Minute?Second?Hour? : ").lower()
user = 'yes'

# Choose The Time
if 'hour' in choose:
    multi = times * 3600
elif 'minute' in choose:
    multi = times * 60
elif 'second' in choose:
    multi = times

# Looping     
while multi > 0 or user == 'yes':
    print(multi)
    time.sleep(1)
    multi -= 1
    if multi == 0:
        print("Sir Its Time For You To Do Your Work And Have Nice Day! :)")        
        user = input("Do You Want To Make More Plans? : ").lower() 
        if user == 'yes':
            plan = input("What Your Plan For Today? : ")
            times = int(input("What Number? : "))
            choose = input("Minute?Second?Hour? : ").lower()
            if 'hour' in choose:
                multi = times * 3600
            elif 'minute' in choose:
                multi = times * 60
            elif 'second' in choose:
                multi = times
        else:
            user = 'no'
I have a code like a reminder to do work. I want the results of the seconds to only print once but the numbers continue to decrease

How to do it?
Reply
#2
Do you mean this?

while multi > 0 or user == 'yes':
    print(multi)
Change to:
print(multi)
while multi > 0 or user == 'yes':
    
And this:

        if user == 'yes':
            plan = input("What Your Plan For Today? : ")
            times = int(input("What Number? : "))
            choose = input("Minute?Second?Hour? : ").lower()
            if 'hour' in choose:
                multi = times * 3600
            elif 'minute' in choose:
                multi = times * 60
            elif 'second' in choose:
                multi = times
            print(multi)              # Added this line
        else:
            user = 'no'
Reply
#3
Some general observations:

Your code has repeating parts (rows #5-16 & #27-35). You should write function instead of writing code blocks twice.

Also you could add functionality to deal with faulty user input. Currently if user enters something which is not resolved within if-elif-elif then name 'multi' will not be assigned.
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#4
(Jan-03-2020, 07:36 AM)perfringo Wrote: Some general observations:

Your code has repeating parts (rows #5-16 & #27-35). You should write function instead of writing code blocks twice.

Also you could add functionality to deal with faulty user input. Currently if user enters something which is not resolved within if-elif-elif then name 'multi' will not be assigned.


ohhh thanks for researching my code Smile


import time
import winsound

# User Input
def choose():
    user = 'yes'
    plan = input("What Your Plan For Today? : ").lower()
    times = int(input("What Number? : "))
    while True:
        choose1 = input("Minute?Second?Hour? : ").lower()
        if 'hour' in choose1:
            multi = times * 3600
            break
        elif 'minute' in choose1:
            multi = times * 60
            break
        elif 'second' in choose1:
            multi = times
            break
        else:
            print("Sorry Sir, There Is No Such Option")

# Looping     
while multi > 0 or user == 'yes':
    print(multi)
    time.sleep(1)
    multi -= 1
    if multi == 0:
        print("Sir Its Time For You To Do Your Work And Have Nice Day! :)")        
        winsound.PlaySound('C:\\Users\\user\\Documents\\All About Python3 Or Programming\\All 
        Project With Python 3\\background.wav', winsound.SND_FILENAME)
        user = input("Do You Want To Make More Plans? : ").lower() 
        if user == 'yes':
            choose()
        else:
            user = 'no'
what do you mean is this?

(Jan-03-2020, 07:23 AM)Killertjuh Wrote: Do you mean this?
while multi > 0 or user == 'yes': print(multi)
Change to:
print(multi) while multi > 0 or user == 'yes': 
And this:
 if user == 'yes': plan = input("What Your Plan For Today? : ") times = int(input("What Number? : ")) choose = input("Minute?Second?Hour? : ").lower() if 'hour' in choose: multi = times * 3600 elif 'minute' in choose: multi = times * 60 elif 'second' in choose: multi = times print(multi) # Added this line else: user = 'no'
Output:
12 11 10 9 8 7 6 5 4 3 2 1
how do I make python print once but the number keeps changing?
Reply
#5
import time
import winsound
 
# User Input
def choose():                            # With a function you have to return your multi var
    user = 'yes'
    plan = input("What Your Plan For Today? : ").lower()
    times = int(input("What Number? : "))
    while True:                   
        choose1 = input("Minute?Second?Hour? : ").lower()
        if 'hour' in choose1:
            multi = times * 3600
            break                 # You dont have to break just use return because you work with function
        elif 'minute' in choose1:
            multi = times * 60
            break                 # You dont have to break just use return because you work with function
        elif 'second' in choose1:
            multi = times
            break                 # You dont have to break just use return because you work with function
        else:
            print("Sorry Sir, There Is No Such Option")
 
# Looping     
while multi > 0 or user == 'yes':         # multi is not defined at this point so this will fail.
    print(multi)                          # you can put this line to linenumber #34
    time.sleep(1)
    multi -= 1                            # multi is not defined so this will fail.
    if multi == 0:
        print("Sir Its Time For You To Do Your Work And Have Nice Day! :)")        
        winsound.PlaySound('C:\\Users\\user\\Documents\\All About Python3 Or Programming\\All 
        Project With Python 3\\background.wav', winsound.SND_FILENAME)
        user = input("Do You Want To Make More Plans? : ").lower() 
        if user == 'yes':
            choose()
        else:
            user = 'no'
I hope this quick change works for you. I`ll didnt test this one.
import time
import winsound

# User Input
def choose():
    user = 'yes'
    plan = input("What Your Plan For Today? : ").lower()
    times = int(input("What Number? : "))
    while True:
        choose1 = input("Minute?Second?Hour? : ").lower()
        if 'hour' in choose1:
            return times * 3600
        elif 'minute' in choose1:
            return times * 60
        elif 'second' in choose1:
            return times
        else:
            print("Sorry Sir, There Is No Such Option")


# Looping
multi = 0
while True:
    time.sleep(1)
    if multi == 0:
        print("Sir Its Time For You To Do Your Work And Have Nice Day! :)")
        winsound.PlaySound('C:\\Users\\user\\Documents\\All About Python3 Or Programming\\All
        Project With Python 3\\background.wav', winsound.SND_FILENAME)
        user = input("Do You Want To Make More Plans? : ").lower()
        if user == 'yes':
            multi = choose()
            print(multi)
        else:
            user = 'no'
            # exit()
    else:
        if isinstance(multi, int):
            multi -= 1
Reply
#6
Just to put some more 'reality' into code:

- user input converted into int. It will throw an error if user enters something which cannot be converted into integer. Especially if user wants to enter something in format hrs:minutes:seconds (1:15:00). People don't tend to think in minutes if timespan is larger than 1 hour.
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Brick Number stored as text with openpyxl CAD79 2 381 Apr-17-2024, 10:17 AM
Last Post: CAD79
  [SOLVED] Pad strings to always get three-digit number? Winfried 2 340 Jan-27-2024, 05:23 PM
Last Post: Winfried
  Prime number detector Mark17 5 801 Nov-27-2023, 12:53 PM
Last Post: deanhystad
  Create X Number of Variables and Assign Data RockBlok 8 928 Nov-14-2023, 08:46 AM
Last Post: perfringo
  find the sum of a series of values that equal a number ancorte 1 494 Oct-30-2023, 05:41 AM
Last Post: Gribouillis
  capturing multiline output for number of parameters jss 3 809 Sep-01-2023, 05:42 PM
Last Post: jss
  Sequential number for rows retrieved and storing the Primary UKey to the line number GYKR 2 575 Aug-22-2023, 10:14 AM
Last Post: GYKR
  doubling a number Skaperen 8 1,213 Jul-25-2023, 10:20 PM
Last Post: Skaperen
Question Extracting Version Number from a String britesc 2 1,087 May-31-2023, 10:20 AM
Last Post: britesc
  Formatting float number output barryjo 2 911 May-04-2023, 02:04 PM
Last Post: barryjo

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020