Jump to content

Archived

This topic is now archived and is closed to further replies.

BosnianArmy

Python programming

Recommended Posts

Hello there folks, long time no speak/topic/forum?

Anyways, I've been studying here and there with school and currently I am learning to program with Python at school and home. Beside Python I study also networking (subnetting and Cisco stuff), Server managing, etc. But we are here for the Python help. The main subject for this little project is that I want to make a small "guessing game" as training.

How the game works:

The game starts with a small storyline and then the program asks how many seconds you need to think for the number that you're about to guess it. After putting the number, the program starts to countdown with a live timer. But the problem is, the program stays with the live timer, doesn't continue with the rest of the code which I'm itching about it how to fix that. But once this is fixed the game should start, you need to guess the number with only 2 guesses (or lifes how ever you see it).

For now I am using greater than and less than function to see if this would work when you enter the right or false number. But seems like what ever number you put in, it will see as a "positive" answer. But it should be that the answer is good or false, good leads of course to you've won the game and false leads to the second guess and from the second guess leads to, Game Over. So here is my code to those who want's to see it:

#Print() = print something
#Integer() = whole numbers --> 5
#Floats = numbers after the comma --> 5.0/5,0
#Strings "" = text --> "Hello World"
#Input() = a user can type something in it
#
#########################################################################
#Introduction storyline
a = "Welcome! Press \"Enter\" to start the game"
print(a)
input() #this should be empty, this leads to the variable "a" from the first line of the code ---> Press enter to start the game

print("First.. a story")
print("Once there was a legend that could guess a number without hesitating, but is that legend true, are you the legend? Let's find out!\n")

#timer before the game starts
import time

while True:
 uin = input(">>How many seconds do you need to guess? Enter here: ")
 try:
 
  when_to_stop = abs(int(uin))
 except KeyboardInterrupt:
		break
 except:
		print("Not a number!")
		
 while when_to_stop > 0:
			m, s = divmod(when_to_stop, 60)
			h, m = divmod(m,60)
			time_left = str(h).zfill(2) + ":" + str(s).zfill(2)
			print(time_left + "\r", end="")
			time.sleep(1)
			when_to_stop -= 1
 print()

#Core code
b = float (input("Guess your number from 0 to 100:"))
c = 3 ** 2
d = c == 9

if d > 9: 
 print("You guessed wrong!\n")
else:
 print("One more chance:")
 

if d < 9:
 print("You guessed wrong!\n")
else:
 print("Game Over!")
 

Thanks for reading and helping :) 

Share this post


Link to post

You need to be very careful with indents in python. Your

while True:

loop repeats forever. However, the only part that is repeating is

while True:
 uin = input(">>How many seconds do you need to guess? Enter here: ")
 try:
 
  when_to_stop = abs(int(uin))
 except KeyboardInterrupt:
		break
 except:
		print("Not a number!")
		
 while when_to_stop > 0:
			m, s = divmod(when_to_stop, 60)
			h, m = divmod(m,60)
			time_left = str(h).zfill(2) + ":" + str(s).zfill(2)
			print(time_left + "\r", end="")
			time.sleep(1)
			when_to_stop -= 1
 print()

Because each line following

#Core code

is not indented.

In most other languages, '{' and '}' are used to identify the code that is included in a loop. In python, no such syntax exists, therefore you have to put an indent (space or tab) before each following line of code that you wish to be included in the loop.

The following works now:

#Print() = print something
#Integer() = whole numbers --> 5
#Floats = numbers after the comma --> 5.0/5,0
#Strings "" = text --> "Hello World"
#Input() = a user can type something in it
#
#########################################################################
#Introduction storyline
a = "Welcome! Press \"Enter\" to start the game"
print(a)
input() #this should be empty, this leads to the variable "a" from the first line of the code ---> Press enter to start the game

print("First.. a story")
print("Once there was a legend that could guess a number without hesitating, but is that legend true, are you the legend? Let's find out!\n")

#timer before the game starts
import time

while True:
 uin = input(">>How many seconds do you need to guess? Enter here: ")
 try:

 when_to_stop = abs(int(uin))
 except KeyboardInterrupt:
		break
 except:
		print("Not a number!")

 while when_to_stop > 0:
			m, s = divmod(when_to_stop, 60)
			h, m = divmod(m,60)
			time_left = str(h).zfill(2) + ":" + str(s).zfill(2)
			print(time_left + "\r", end="")
			time.sleep(1)
			when_to_stop -= 1
 print()

 #Core code
 b = float (input("Guess your number from 0 to 100:"))
 c = 3 ** 2
 d = c == 9

 if d > 9:
     print("You guessed wrong!\n")
 else:
     print("One more chance:")

 if d < 9:
     print("You guessed wrong!\n")
 else:
     print("Game Over!")

Although there are still umpteen errors/bugs that cause the program to not run in the way you desire. You can figure that out yourself. Good luck.

Share this post


Link to post

×