Python Forum
Code: Creating a basic python game? - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Homework (https://python-forum.io/forum-9.html)
+--- Thread: Code: Creating a basic python game? (/thread-14028.html)



Code: Creating a basic python game? - searching1 - Nov-12-2018

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


RE: Code: Creating a basic python game? - ichabod801 - Nov-12-2018

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.


RE: Code: Creating a basic python game? - searching1 - Nov-12-2018

@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



RE: Code: Creating a basic python game? - stullis - Nov-12-2018

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.


RE: Code: Creating a basic python game? - ichabod801 - Nov-12-2018

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.


RE: Code: Creating a basic python game? - searching1 - Nov-12-2018

Thanks All for your input... Its now working :)