Python Forum
Code: Creating a basic python game?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Code: Creating a basic python game?
#1
Hi, I'm practicing coding using python and I'm working with this small project of mine. Im having issue running or calling the function into if else.

Target :
I want to generate a random between 0-900 and define a range,
if 0-300 then its 1 that represent rock, 301-600 is paper and 601-900 is scissors.. then player will select 1-3 for (rock paper scissors) if it matches tie. Thanks

Heres my code:
print ("Welcome to RPS GAME, Winning rules as follows: \n"
       +"Rock vs Paper = Paper Wins \n"
       +"Paper vs Scissor  = Scissor Wins \n"
       +"Scissor vs Rock = Rock Wins \n" )

#CPU Random
cpurange = range(300)
cpu = random.choice(cpurange)

def getvalue(cpu):
   if cpu <= 300:
       getvalue = 1

#Human will select
human = int(input ("Pick 0-Rock, 1-Paper, 2-Scissors?: "))

if human == 1 and getvalue == 1:
  print ("TIE")

print ("##### RESULT ##### \n"
       +"Human: {}".format(human))
Result is not working if thinking if I properly calling the def getvalue function into below if/else statement.

Thanks
Reply
#2
Please use python and output tags when posting code and results. I put them in for you this time. Here are instructions for doing it yourself next time.

The first problem is that you have getvalue serving two purposes: The name of the function and the name of the value it creates. Second, the value it creates exists only in the function. You need to return it to access it outside the function:

def getvalue(cpu):
    if cpu <= 300:
        value = 1
    return value

computer = getvalue(cpu)
Note that the easiest way to get a random value from 1 to 3 is random.randint(1, 3). Then you don't need a separate function. Especially because getvalue as set up now will cause an error if cpu is over 300.

Note that there is a tie whenever the human and computer choose the same, not just when they both choose 1 (human == computer). Also triple quotes (""") can be used to start and end a multi-line string.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
@ichabod801, Thanks for your reply.

Standard procedure of posting of codes is noted.

1. While the given command works, But I would like to if to define the function is we need to create a variable "computer"? (Will research about funnction tho.)

2. random.randint(1, 3) - I this case its 1 to 3 only but i need 0 - 900 then I divide it to 3 for 3 choices (rock,paper,scissor). Should I use (1,900)?

3.
def getvalue(cpu):
   if cpu <= 300:
       value = 1
   elif cpu == range(301,600):
       value = 2
   return value

computer = getvalue(cpu)

if human == 1 and computer == 1:
          print ("TIE-ROCK")
elif human == 2 and computer == 2:
          print ("TIE-PAPER")
Currently its not working when more than 300 as stated above.

Tried using this, but completely blind on what will is use to get the range of 300 to 600 and assign a value of 2.. Checking on conjunction operator
#CPU Random
rndm = random.randint(301,600)

def getvalue(rndm):
   if rndm <= 300:
       value = 1
   elif rndm >=301 and <=600:
       value = 2
   elif rndm <= 601:
       value = 3
   return value
Reply
#4
Your current code has syntax errors. In the first version on line 4, you are checking if cpu equals a range object; this is failing for two reasons. First, cpu is a number, not a range object so it will never be equal. The intent is to check if cpu is within the range, not that it's equal to the range. There are two ways to write this:

def getvalue(cpu):
   if cpu <= 300:
       value = 1
   elif cpu in range(301,600):
       value = 2
   else: value = 3

   return value
or

def getvalue(cpu):
   if cpu <= 300:
       value = 1
   elif 301 <= cpu <= 600:
       value = 2
   else: value = 3

   return value
On a side note, a random number between 1 and 900 is excessive. You truly only need one through three since there are only three possibilities.
Reply
#5
Also note that if a value is not 300 or less, then you already know it's over 300:

def getvalue(cpu):
    if cpu <= 300:
        value = 1
    elif cpu <= 600:
        value = 2
    else:
        value = 3
    return value
Of course, since it is even ranges, you could just divide (random.randint(1, 900) // 3 + 1).

But as I implied and stulis clarified, generating 1-900 and then shrinking it down to 1-3 is overkill. Unless that is part of the assignment?

Have you done dictionaries yet? That's the best way to handle determining who won rock-paper-scissors in my experience.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#6
Thanks All for your input... Its now working :)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Need some help creating a word game wthiierry 4 2,405 Nov-01-2022, 12:29 PM
Last Post: perfringo
  problem on creating a small game nayo43 5 2,752 Dec-13-2020, 01:03 PM
Last Post: jefsummers
  [split] help me make this code better please (basic) Rustam 2 2,264 Jun-19-2020, 01:27 PM
Last Post: Rustam
  restarting game code zyada7med 5 4,638 Sep-03-2019, 09:24 PM
Last Post: ichabod801
  Asking for help in my code for a "Guess the number" game. Domz 5 3,813 Aug-14-2019, 12:35 PM
Last Post: perfringo
  Basic Quiz/Game searching1 10 5,664 Nov-18-2018, 03:58 AM
Last Post: ichabod801
  Creating code to make up to 4 turtle move simultaneously in a random heading J0k3r 3 5,490 Mar-05-2018, 03:48 PM
Last Post: mpd
  Basic code HGOBOI 2 2,758 Mar-05-2018, 12:02 PM
Last Post: Larz60+
  Lots of code. Creating modules jwhenson1990 2 2,837 Oct-13-2017, 08:31 AM
Last Post: gruntfutuk
  Hangman-Game (German code) .. Unkreatief 1 3,721 Mar-22-2017, 10:30 AM
Last Post: Kebap

Forum Jump:

User Panel Messages

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