Python Forum
Checking input is close to random num
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Checking input is close to random num
#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
Reply


Messages In This Thread
RE: Checking input is close to random num - by Tomington - Feb-11-2019, 09:44 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Matplotlib - close multple plots with user input Positron79 0 1,755 Dec-01-2021, 05:26 PM
Last Post: Positron79
  Generate Random operator, take user input and validate the user mapypy 4 5,575 Feb-03-2021, 08:41 PM
Last Post: nilamo
  Calling Input for Random Generation ScaledCodingWarrior 1 1,865 Feb-02-2021, 07:27 PM
Last Post: bowlofred
  trying to input a variable using random.choice python63 9 3,653 Aug-13-2020, 05:37 PM
Last Post: python63
  Guess Random Number Why i m not able to enter input Nithya Thiyagarajan 6 8,233 Jan-07-2018, 04:26 AM
Last Post: squenson

Forum Jump:

User Panel Messages

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