Number Guess Problem


#1

I have been making a version of the number guess game.
All my code works but the way the program finds if it needs to print if its hotter or colder then the last guess
So basically I want to write in python 'is Guess1 closer to Number than Guess2'
Guess1=The last guess made
Number=The number you are looking for
Guess2=The guess your comparing to

Here is my code:

import time
import sys
import math
from random import randint
start = time.time()
Number=randint(0,100000)
Guess1=0
Guess2=randint(0,100000)
Cheat=randint(5000,200000)
counter=0

print("The game is loading")
time.sleep(10)
name=input("Please print your name ")
print("")
if name == "Bob":
print("")
print("Hello Owner")
print("The In Game Cheat Code Is")
print(Cheat,)
print("")
print("Hello",name,)
print("If you don't know already you are playing Number Guess")
print("To play when asked, print the number")
print("Soon after the computer will tell you if the number is hotter or colder than the last guess")
print("It will not say hot or cold on the first guess")
print("The number is between 0 and 100,000")
print("You will have 300 seconds to find the number")
print("Good Luck")
print("")
age=int(input("Please print your age, to start "))
if age == 11:
print("Your aged in perfection")
if age < 18:
print("To Young, this game is 18+")
sys.exit()
elif age >= 100:
print("Why cheat?")
sys.exit()
else:

while True:
start = time.time()
Number=randint(0,100000)
Guess1=0
Guess2=randint(0,100000)

  print("")
  print("Number Guess ™")
  print("© 2015-2016 Daniel Smith")
  print("All rights reserved.\n")
  print("")
  won = 0
  print("Hello,",name,)
  print("A new number has been chosen.")
  print("Notice:Only whole numbers work(No negative numbers or decimals)")
  print("The timer has started.")
  i = 0
  while (Number!=Guess1) and (time.time() - start < 300):
      print("You have",math.trunc(300 - (time.time() - start)),"seconds left.")
      print("Notice: The number is between 0 and 100,000\n")
      Guess1=int(input("Guess:"))
      if Cheat==Guess1:
          print("You found the number!")
          print("The number was",Number)
          print("Notice:This game is imposible to win. (It just keeps restarting)\n")
          won = 1
          break
      print("\n")
      if(Guess1 != Number):
          if i == 0:
              print("Wrong Guess")
              i = 1
              Guess2 = Guess1
              continue

          if  Guess1 > Number:
              if ((Guess1>Guess2) and (Guess1>Number)):   #This is the bit
                  print ("You are getting colder than the last guess.")
                  print("")
                  Guess2 = Guess1
              elif ((Guess1<Guess2) and (Guess1>Number)):     #this is another bit
                  print("You are getting hotter than the last guess.")   
                  print("")                                                   
                  Guess2 = Guess1
          elif Guess1< Number:                                              
              if ((Guess1>Guess2) and (Guess1<Number)): #another bit
                  print("You are getting hotter than the last guess.") 
                  print("")
                  Guess2 = Guess1
              elif((Guess1<Guess2) and (Guess1<Number)): #Another bit
                  print ("You are getting colder than the last guess.")
                  print("")
                  Guess2 = Guess1
      elif(Guess1==Number):
              counter += 1
              print("You found the number!")
              print("You had" ,math.trunc(300 - (time.time() - start)),"seconds left.")
              print("It's been your",counter,"attempt to complete the game")
              print("Notice: This game is imposible to win. (It just keeps restarting)\n")
              print("The next round is loading...")
              counter=0
              time.sleep(10)
              won = 1

              break

  if won == 0:
      counter += 1
      print("Time's up the number was!",Number)
      print("It'a been your",counter,"attempt to complete the game")
      print("The next round is loading...")
      time.sleep(10)

Can you help?


#2

@riblo26,

'''
if the absolute distance 
to the number to guess
of the current-choice is =smaller than=
the prior-choice
then display "warmer"
otherwise display "colder"
'''

#then 'right' number
to_choose = 20
#all-choices as test-case
choices=[5,10,22,25,18,16,19,20]
#prior choice variable to keep track of prior choice
pch=0
for choice in choices:
    if choice == to_choose:
        print("=========\n|| Perfect ||\nthe-number:{}"
              .format(to_choose))
    elif abs(to_choose-choice) < abs(to_choose-pch):
        print(
            "[{0}] pch:{1} choice:{2} delta:{3}"
            .format(to_choose,pch,choice,"warmer"))
    else:
        print(
            "[{0}] pch:{1} choice:{2} delta:{3}"
            .format(to_choose,pch,choice,"colder"))
        
    pch=choice

#3

Hello

I applied the code to my work
I ended up with this:

import time
import sys
import math
from random import randint
start = time.time()
Number=randint(0,100000)
Guess1=0
Guess2=randint(0,100000)
Cheat=randint(5000,200000)
counter=0

print("The game is loading")
time.sleep(10)
name=input("Please print your name ")
print("")
if name == "Bob":
print("")
print("Hello Owner")
print("The In Game Cheat Code Is")
print(Cheat,)
print("")
print("Hello",name,)
print("If you don't know already you are playing Number Guess")
print("To play when asked, print the number")
print("Soon after the computer will tell you if the number is hotter or colder than the last guess")
print("It will not say hot or cold on the first guess")
print("The number is between 0 and 100,000")
print("You will have 300 seconds to find the number")
print("Good Luck")
print("")
age=int(input("Please print your age, to start "))
if age == 11:
print("Your aged in perfection")
if age < 18:
print("To Young, this game is 18+")
sys.exit()
elif age >= 100:
print("Why cheat?")
sys.exit()
else:

while True:
start = time.time()
Number=randint(0,100000)
Guess1=0
Guess2=randint(0,100000)

  print("")
  print("Number Guess ™")
  print("© 2015-2016 Daniel Smith")
  print("All rights reserved.\n")
  print("")
  won = 0
  print("Hello,",name,)
  print("A new number has been chosen.")
  print("Notice:Only whole numbers work(No negative numbers or decimals)")
  print("The timer has started.")
  i = 0
  while (Number!=Guess1) and (time.time() - start < 300):
      print("You have",math.trunc(300 - (time.time() - start)),"seconds left.")
      print("Notice: The number is between 0 and 100,000\n")
      Guess1=int(input("Guess:"))
      if Cheat==Guess1:
          print("You found the number!")
          print("The number was",Number)
          print("Notice:This game is imposible to win. (It just keeps restarting)\n")
          won = 1
          break
      print("\n")

then 'right' number

to_choose = 20

all-choices as test-case

choices=[5,10,22,25,18,16,19,20]

prior choice variable to keep track of prior choice

pch=0
for choice in choices:
if choice == to_choose:
print("=========\n|| Perfect ||\nthe-number:{}"
print("You found the number!")
print("You had" ,math.trunc(300 - (time.time() - start)),"seconds left.")
counter=0
won=1
print("It's been your",counter,"attempt to complete the game")
print("Notice: This game is imposible to win. (It just keeps restarting)\n")
print("The next round is loading...")
time.sleep(10)
break
.format(to_choose))
elif abs(to_choose-choice) < abs(to_choose-pch):
print(
"[{0}] pch:{1} choice:{2} delta:{3}"
.format(to_choose,pch,choice,"warmer"))
else:
print(
"[{0}] pch:{1} choice:{2} delta:{3}"
.format(to_choose,pch,choice,"colder"))

pch=choice

  if won == 0:
      counter += 1
      print("Time's up the number was!",Number)
      print("It'a been your",counter,"attempt to complete the game")
      print("The next round is loading...")
      time.sleep(10)

It didn't work
can you help?


#4

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.