0
# code 1
import time
while True:
    from datetime import datetime
    print(" Time: "+ "%d:%d:%d " % (datetime.now().hour,datetime.now().minute,datetime.now().second),
    end = "\r")
    time.sleep(1)



# code 2
name = input("Your good name please: ")
print("Hi "+name)
print('''Lets Play Guessing number game
    ''')

import random
import math
print("you have only 1 chance to guess the number")
original_no = random.randint(0,10)
player_input = input("Guess a number from 1 to 10: ")
if str(player_input) == original_no:
    print("you have entered the correct number!")
elif str(player_input) != original_no:
    print("you have entered wrong number")
print(str("the correct number is ") + str(original_no))

when the code gets executed, it only prints the code 1 and doesn't print the code 2. I want to display time over the number guessing game. I'm just a beginner ,please help me resolve this issue.

  • Did you ever ended the `while` loop? – Utpal Kumar Jul 07 '21 at 04:03
  • You will need some kind of GUI framework if you want to update two different parts of the screen at the same time. – Mark Ransom Jul 07 '21 at 04:05
  • 1
    You have two options if you want to do this. Firstly, you can do what Mark mentioned and use a GUI framework. Second option is to run your time loop in a separate thread / asynchronous process and have a screen update function that clears the screen and writes the entire output again each time. However, Mark's suggestion is probably the best way to go about it. – hyper-neutrino Jul 07 '21 at 04:06
  • @hyper-neutrino thanks for your help! – Shiv Akash Jul 09 '21 at 03:30

1 Answers1

0

You need to end the while loop, you can do it by simply including a break statement in the loop or just remove while loop in case you want the time to be displayed only once.

while True:
    from datetime import datetime
    print(" Time: "+ "%d:%d:%d " % (datetime.now().hour,datetime.now().minute,datetime.now().second),
    end = "\r")
    break
    time.sleep(1)
NIKITA RATH
  • 354
  • 1
  • 3
  • 12