Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Checking input is close to random num
#1
G,day everyone

so i'm reasonably new to python coding, I've not done very much and am looking for some help. the code below is guessing number game my teacher got my class to do as a learning exercise. its not homework either. i'm just trying to learn more. so what I'm trying to achieve is when the players choice gets close to the computers choice. I want to say "Getting Warm", "Getting hot" or if the players choice is far from the computers choice "cold", "very cold". I don't know how to check if the player choice is close the computers choice and then let them know. I'm not looking for an answer exactly just something to put me on the right path, if i know if i know how to check if pc is in range of cc by 10 for example i cant then print getting hot or hotter. I appreciate your time, Thanks

import random
#variable names
#cc = Computers Choice
#pc = Players Choice

cc = random.randint(1,10)
endgame = False
attempts = 0
yes = True

while(yes==True):
    while(endgame==False):
        pc = int(input("choose a number between 1 -10 "))
        attempts = attempts+1
#determines if the player choice is the right choice then ends the game
        if(pc==cc):
            print("well done you guessed correctly")
            print ("Total Attempts Made " + str(attempts))
            endgame = True
#determining if the player choice is higher or lower and letting them know what to further guess for
        elif(pc<cc):
            print("Higher")
        else:
            print("lower")
#Based on the amount attempts made determines wherether the game ends           
        if(attempts==5):
            endgame = True
            print("You loose")
#Winning a reward based on the attempts made by the player            
        if attempts == 1 and endgame == True:
            print("You Have Won a Car!!!")
        elif attempts == 2 and endgame ==True:
            print("You Have Won a Boat!!!")
        elif attempts == 3 and endgame == True:
            print ("You Have Won a Holidy!!!")
#how the player chooses if they wish to play again or not            
    restart = input("Do you wan't to play again, print 'yes'-'y' or 'no'-'n' ")
    if restart == "yes" or restart == "y":
        yes = True
        endgame = False
        attempts = 0
        cc = random.randint(1,10)
    if restart == "no" or restart == "n":
        yes = False
        print("Thanks for playing")
    
Quote
#2
The abs function is the absolute value. The absolute value of the difference between the guess and the target is how close they are.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures

Quote
#3
G,day ichabod801

Thanks for your reply. I've added to my code with the abs function and got the hot, hotter and cold working. ill post the updated code.
import random
#variable names
#cc = Computers Choice
#pc = Players Choice


cc = random.randint(1,20)
endgame = False
attempts = 0
yes = True

while(yes==True):
    while(endgame==False):
        pc = int(input("choose a number between 1 & 20 "))
        gap = abs(pc-cc)
        #print(gap)
        attempts = attempts+1
#determines if the player choice is the right choice then ends the game
        if(pc==cc):
            print("well done you guessed correctly")
            print ("Total Attempts Made " + str(attempts))
            endgame = True
#determining if the player choice is hotter or colder 
        elif(gap<4):
            print("Very Hot")
        elif(gap<8):
            print("Getting Hot")
        elif(gap<12):
            print("Cold")
        elif pc<=1 or pc>=20:
            print("Please input A number between 1 & 20")
#Based on the amount attempts made determines wherether the game ends           
        if(attempts==5):
            endgame = True
            print("You lose")
#Winning a reward based on the attempts made by the player            
        if attempts == 1 and endgame == True:
            print("You Have Won a Car!!!")
        elif attempts == 2 and endgame ==True:
            print("You Have Won a Boat!!!")
        elif attempts == 3 and endgame == True:
            print ("You Have Won a Holidy!!!")
#how the player chooses if they wish to play again or not            
    restart = input("Do you wan't to play again, print 'yes'-'y' or 'no'-'n' ")
    if restart == "yes" or restart == "y":
        yes = True
        endgame = False
        attempts = 0
        cc = random.randint(1,20)
    if restart == "no" or restart == "n":
        yes = False
        print("Thanks for playing")
I think i'm using the abs function correctly although taking the abs function out of the 'gap' variable also works. Either way I've learned about the abs function and got what i was looking for to work so i appreciate your help ichabod801
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  How to import random? 1nc1 0 19 2 hours ago
Last Post: 1nc1
  Help with ValueError using ranint from random. FWendeburg 2 55 Feb-14-2019, 04:22 AM
Last Post: ichabod801
  3 random numbers MrGoat 1 123 Jan-15-2019, 06:33 AM
Last Post: Larz60+
  from 50 random numbers printing only the sqrt(...)==0 AFKManager 1 123 Jan-12-2019, 08:16 PM
Last Post: ichabod801
  Save and Close and Excel sheet with pre defined name Vinci141 1 176 Nov-28-2018, 06:14 AM
Last Post: Vinci141
  random number anasa 2 219 Nov-26-2018, 08:56 PM
Last Post: Excelsiscelia
  Animating Random Walk Excelsiscelia 2 285 Nov-24-2018, 08:53 AM
Last Post: Excelsiscelia
  How do I create a user input for three integers in a range or just the stop input Pleiades 3 244 Nov-22-2018, 02:19 PM
Last Post: Larz60+
  Random Expression Creator SheeppOSU 2 223 Nov-01-2018, 11:08 PM
Last Post: wavic
  Random module Python 3.6 daryl_uk 1 222 Oct-25-2018, 12:09 AM
Last Post: micseydel

Forum Jump:


Users browsing this thread: 1 Guest(s)