Jump to content

need help with python program

Celious
Go to solution Solved by LtStaffel,

Here is a python 2.7 version of your script (because I'm using PyPy which is 2.7 style syntax)

Please mark this post as the answer so the thread is "solved"

 

Spoiler

from random import *

def answer():
    # You don't need elif, it either will be 1 or zero so only make one check and use else
    x = (randint(0, 1))
    if x == 1:
        return "HEADS"
    else:
        return "TAILS"
        # You were returning the parameter given to the function, when the function didn't need a parameter
        # You were also printing the result instead of returning it

playerChoice = str(raw_input("Heads or Tails: ")).upper()
if playerChoice == 'HEADS' or 'TAILS':
    # You didn't indent properly
    print "You chose: " + playerChoice
    # You don't need to wait a split second
    # Also, the computer doesn't get a choice if the player chooses
    # Here we get the result using your function "answer"
    ans = answer()
    print "You chose  ::: " + playerChoice
    print "The answer ::: " + ans
    if playerChoice == ans:
        print "You won!"
    else:
        print "You lost!"
else:
    print "Invalid input!"

 

 

 

Trying to make a Heads or Tails program in Python, here is what I have right now:
                                                                                                                                                


 

import time
from random import *
def computerChoice(z):
    Z = (randint(0,1))
    if Z == 1:
        print("Heads")
    elif Z == 0:
        print ("Tails")
    return(z)
    

def anwser(x):
    x = (randint(0,1))
    if x == 1:
        print("Heads")
    elif x == 0:
        print ("Tails")
    return (x)

playerChoice = input("Heads or Tails: ").upper()
if playerChoice == 'HEADS' or 'TAILS':
   
print ("You chose:", playerChoice)
    time.sleep(0.1)
    print ("Computer chose:", computerChoice)                   





 The problem is that at the last line instead of printing 'Heads' or 'Tails' it prints this:      <function computerChoice at 0x030DFF18>             

Link to comment
Share on other sites

Link to post
Share on other sites

You forgot the parentheses. It should be :

print("Computer chose:", computerChoice())

You should also remove x and z from your function definitions as they are not needed (those are parameters, not return variables).

My current build :

Hurricane Mk.I

  • CPU - Intel Core i5-4690K @ 4.6 GHz ~ 1.25V
  • Cooling - Corsair H60 2013 Edition
  • Motherboard - Gigabyte Z97MX-Gaming 5 (mATX)
  • RAM - 16 GB DDR3-1600 Avexir Core Series (Red LEDs)
  • GPU - XFX Radeon R9 Fury X @ 1100/500 MHz -24 mV
  • Case - CoolerMaster MasterCase Pro 3
  • SSD - 256 GB ADATA SX900
  • HDD - 1 TB Seagate Barracuda 7200 RPM
  • PSU - Corsair RM750 (750W 80+ Gold)
  • Display - LG 34UM67-P Ultrawide (Freesync)
  • Keyboard - CM Storm Quickfire TK (Cherry MX Red) + CM Masterkeys Pro S RGB (Cherry MX Blue)
  • Mouse - Razer DeathAdder 2013
  • Audio - Sennheiser HD598 SE with detachable mic
  • OS - Windows 10 Home Edition
  • VR - Lenovo Explorer Windows Mixed Reality Headset
  • Laptop - ASUS K501LX with i7-5500U and GTX 950M
Link to comment
Share on other sites

Link to post
Share on other sites

And you should also use code tags

Join the Appleitionist cause! See spoiler below for answers to common questions that shouldn't be common!

Spoiler

Q: Do I have a virus?!
A: If you didn't click a sketchy email, haven't left your computer physically open to attack, haven't downloaded anything sketchy/free, know that your software hasn't been exploited in a new hack, then the answer is: probably not.

 

Q: What email/VPN should I use?
A: Proton mail and VPN are the best for email and VPNs respectively. (They're free in a good way)

 

Q: How can I stay anonymous on the (deep/dark) webzz???....

A: By learning how to de-anonymize everyone else; if you can do that, then you know what to do for yourself.

 

Q: What Linux distro is best for x y z?

A: Lubuntu for things with little processing power, Ubuntu for normal PCs, and if you need to do anything else then it's best if you do the research yourself.

 

Q: Why is my Linux giving me x y z error?

A: Have you not googled it? Are you sure StackOverflow doesn't have an answer? Does the error tell you what's wrong? If the answer is no to all of those, message me.

 

Link to comment
Share on other sites

Link to post
Share on other sites

3 minutes ago, LtStaffel said:

And you should also use code tags

You mean hash?

Link to comment
Share on other sites

Link to post
Share on other sites

Just now, Celious said:

You mean hash?

He means in your post, you can use the " <> " button in the upper bar to insert formatted code like I did.

My current build :

Hurricane Mk.I

  • CPU - Intel Core i5-4690K @ 4.6 GHz ~ 1.25V
  • Cooling - Corsair H60 2013 Edition
  • Motherboard - Gigabyte Z97MX-Gaming 5 (mATX)
  • RAM - 16 GB DDR3-1600 Avexir Core Series (Red LEDs)
  • GPU - XFX Radeon R9 Fury X @ 1100/500 MHz -24 mV
  • Case - CoolerMaster MasterCase Pro 3
  • SSD - 256 GB ADATA SX900
  • HDD - 1 TB Seagate Barracuda 7200 RPM
  • PSU - Corsair RM750 (750W 80+ Gold)
  • Display - LG 34UM67-P Ultrawide (Freesync)
  • Keyboard - CM Storm Quickfire TK (Cherry MX Red) + CM Masterkeys Pro S RGB (Cherry MX Blue)
  • Mouse - Razer DeathAdder 2013
  • Audio - Sennheiser HD598 SE with detachable mic
  • OS - Windows 10 Home Edition
  • VR - Lenovo Explorer Windows Mixed Reality Headset
  • Laptop - ASUS K501LX with i7-5500U and GTX 950M
Link to comment
Share on other sites

Link to post
Share on other sites

9 minutes ago, Fire Yoshi said:

You forgot the parentheses. It should be :


print("Computer chose:", computerChoice())

You should also remove x and z from your function definitions as they are not needed (those are parameters, not return variables).

alright i did that this is what i updated the code to be:

 

import time
from random import *
def computerChoice():
    Z = (randint(0,1))
    if Z == 1:
        print("Heads")
    elif Z == 0:
        print ("Tails")
    return(z)
    

def anwser():
    x = (randint(0,1))
    if x == 1:
        print("Heads")
    elif x == 0:
        print ("Tails")
    return (x)
playerChoice = input("Heads or Tails: ").upper()
if playerChoice == 'HEADS' or 'TAILS':
    print ("You chose:", playerChoice)
    time.sleep(0.1)
    print ("Computer chose:", computerChoice())


However it still comes up with an error, which is:

NameError: name 'z' is not defined

Link to comment
Share on other sites

Link to post
Share on other sites

Here is a python 2.7 version of your script (because I'm using PyPy which is 2.7 style syntax)

Please mark this post as the answer so the thread is "solved"

 

Spoiler

from random import *

def answer():
    # You don't need elif, it either will be 1 or zero so only make one check and use else
    x = (randint(0, 1))
    if x == 1:
        return "HEADS"
    else:
        return "TAILS"
        # You were returning the parameter given to the function, when the function didn't need a parameter
        # You were also printing the result instead of returning it

playerChoice = str(raw_input("Heads or Tails: ")).upper()
if playerChoice == 'HEADS' or 'TAILS':
    # You didn't indent properly
    print "You chose: " + playerChoice
    # You don't need to wait a split second
    # Also, the computer doesn't get a choice if the player chooses
    # Here we get the result using your function "answer"
    ans = answer()
    print "You chose  ::: " + playerChoice
    print "The answer ::: " + ans
    if playerChoice == ans:
        print "You won!"
    else:
        print "You lost!"
else:
    print "Invalid input!"

 

 

 

Join the Appleitionist cause! See spoiler below for answers to common questions that shouldn't be common!

Spoiler

Q: Do I have a virus?!
A: If you didn't click a sketchy email, haven't left your computer physically open to attack, haven't downloaded anything sketchy/free, know that your software hasn't been exploited in a new hack, then the answer is: probably not.

 

Q: What email/VPN should I use?
A: Proton mail and VPN are the best for email and VPNs respectively. (They're free in a good way)

 

Q: How can I stay anonymous on the (deep/dark) webzz???....

A: By learning how to de-anonymize everyone else; if you can do that, then you know what to do for yourself.

 

Q: What Linux distro is best for x y z?

A: Lubuntu for things with little processing power, Ubuntu for normal PCs, and if you need to do anything else then it's best if you do the research yourself.

 

Q: Why is my Linux giving me x y z error?

A: Have you not googled it? Are you sure StackOverflow doesn't have an answer? Does the error tell you what's wrong? If the answer is no to all of those, message me.

 

Link to comment
Share on other sites

Link to post
Share on other sites

7 minutes ago, Celious said:

alright i did that this is what i updated the code to be:

 

import time
from random import *

def computerChoice():
    Z = (randint(0,1))
    if Z == 1:
        print("Heads")
    elif Z == 0:
        print ("Tails")
    return(z)
    


def anwser():
    x = (randint(0,1))
    if x == 1:
        print("Heads")
    elif x == 0:
        print ("Tails")
    return (x)

playerChoice = input("Heads or Tails: ").upper()

if playerChoice == 'HEADS' or 'TAILS':
    print ("You chose:", playerChoice)
    time.sleep(0.1)
    print ("Computer chose:", computerChoice())


However it still comes up with an error, which is:

NameError: name 'z' is not defined

import time
from random import *

def computerChoice():
    if (randint(0,1) == 1):
        return "Heads"
    else:
        return "Tails"

def playerChoice():
    while True:
        playerChoice = input("Heads or Tails: ").upper()
        if playerChoice == 'HEADS' or playerChoice == 'TAILS':
            return playerChoice
        else:
            print("That is not an answer.")
    
playerChoiceAnswer = playerChoice()
print("You chose:", playerChoiceAnswer)
time.sleep(0.1)
print ("Computer chose:", computerChoice())    

 

Link to comment
Share on other sites

Link to post
Share on other sites

2 minutes ago, tjcater said:

-snip for space-

 

Beat you by two lines :D

Join the Appleitionist cause! See spoiler below for answers to common questions that shouldn't be common!

Spoiler

Q: Do I have a virus?!
A: If you didn't click a sketchy email, haven't left your computer physically open to attack, haven't downloaded anything sketchy/free, know that your software hasn't been exploited in a new hack, then the answer is: probably not.

 

Q: What email/VPN should I use?
A: Proton mail and VPN are the best for email and VPNs respectively. (They're free in a good way)

 

Q: How can I stay anonymous on the (deep/dark) webzz???....

A: By learning how to de-anonymize everyone else; if you can do that, then you know what to do for yourself.

 

Q: What Linux distro is best for x y z?

A: Lubuntu for things with little processing power, Ubuntu for normal PCs, and if you need to do anything else then it's best if you do the research yourself.

 

Q: Why is my Linux giving me x y z error?

A: Have you not googled it? Are you sure StackOverflow doesn't have an answer? Does the error tell you what's wrong? If the answer is no to all of those, message me.

 

Link to comment
Share on other sites

Link to post
Share on other sites

Just now, LtStaffel said:

Beat you by two lines :D

Just gotta put it all on one line :P

Link to comment
Share on other sites

Link to post
Share on other sites

Just now, tjcater said:

Just gotta put it all on one line :P

With python you'll need at list two for any script of value

Join the Appleitionist cause! See spoiler below for answers to common questions that shouldn't be common!

Spoiler

Q: Do I have a virus?!
A: If you didn't click a sketchy email, haven't left your computer physically open to attack, haven't downloaded anything sketchy/free, know that your software hasn't been exploited in a new hack, then the answer is: probably not.

 

Q: What email/VPN should I use?
A: Proton mail and VPN are the best for email and VPNs respectively. (They're free in a good way)

 

Q: How can I stay anonymous on the (deep/dark) webzz???....

A: By learning how to de-anonymize everyone else; if you can do that, then you know what to do for yourself.

 

Q: What Linux distro is best for x y z?

A: Lubuntu for things with little processing power, Ubuntu for normal PCs, and if you need to do anything else then it's best if you do the research yourself.

 

Q: Why is my Linux giving me x y z error?

A: Have you not googled it? Are you sure StackOverflow doesn't have an answer? Does the error tell you what's wrong? If the answer is no to all of those, message me.

 

Link to comment
Share on other sites

Link to post
Share on other sites

7 minutes ago, tjcater said:

if playerChoice == 'HEADS' or 'TAILS':

This should be write as:

if playerChoice == 'HEADS' or playerChoice == 'TAILS':

As the argument works like this:

if (playerChoice == 'HEADS') or ('TAILS'):

With the condition 'TAILS', the argument is always satisfied, thus any input works.

Link to comment
Share on other sites

Link to post
Share on other sites

3 minutes ago, tjcater said:

This should be write as:


if playerChoice == 'HEADS' or playerChoice == 'TAILS':

As the argument works like this:


if (playerChoice == 'HEADS') or ('TAILS'):

With the condition 'TAILS', the argument is always satisfied, thus any input works.

Another, longer, option might be:

choices = ['HEADS', 'TAILS', 'EXIT']
if playerChoice in choices:
	blah blah blah

which allows for exiting the game

Join the Appleitionist cause! See spoiler below for answers to common questions that shouldn't be common!

Spoiler

Q: Do I have a virus?!
A: If you didn't click a sketchy email, haven't left your computer physically open to attack, haven't downloaded anything sketchy/free, know that your software hasn't been exploited in a new hack, then the answer is: probably not.

 

Q: What email/VPN should I use?
A: Proton mail and VPN are the best for email and VPNs respectively. (They're free in a good way)

 

Q: How can I stay anonymous on the (deep/dark) webzz???....

A: By learning how to de-anonymize everyone else; if you can do that, then you know what to do for yourself.

 

Q: What Linux distro is best for x y z?

A: Lubuntu for things with little processing power, Ubuntu for normal PCs, and if you need to do anything else then it's best if you do the research yourself.

 

Q: Why is my Linux giving me x y z error?

A: Have you not googled it? Are you sure StackOverflow doesn't have an answer? Does the error tell you what's wrong? If the answer is no to all of those, message me.

 

Link to comment
Share on other sites

Link to post
Share on other sites

34 minutes ago, Celious said:

 


def computerChoice():
    Z = (randint(0,1))
    if Z == 1:
        print("Heads")
    elif Z == 0:
        print ("Tails")
    return(z)


However it still comes up with an error, which is:

NameError: name 'z' is not defined

Well I guess we didn't answer that question for you :S, sorry about that, the reason why is that you defined "Z" and not "z". Python is case sensitive.

Link to comment
Share on other sites

Link to post
Share on other sites

Just now, Celious said:

alright thanks, how do i mark this as solved?

Click on the check mark beside the share button (If you hover over it, it will say marked as solved) on what you think is the answer.

Link to comment
Share on other sites

Link to post
Share on other sites

You might of saw me already with an earlier version of this program, I have now finished it but there is one bug.
Everything works fine but the "You won" and "You lost" statements, it seems to be kinda random. But here's the code:
INFORMATION: PYTHON 3.6.2
 

import time
from random import *

def computerChoice():
    Z = (randint(0,1))
    if Z == 1:
        return("HEADS")
    elif Z == 0:
        return ("TAILS")
    
    


def anwser():
    x = (randint(0,1))
    if x == 1:
        return("HEADS")
    elif x == 0:
        return ("TAILS")
    

playerChoice = input("Heads or Tails: ").upper()

if playerChoice == 'HEADS' or playerChoice == 'TAILS':
    print ("You chose:", playerChoice)
    time.sleep(0.2)
    print ("Computer chose:", computerChoice())
    time.sleep(0.2)
    print("The anwser was: ", anwser())
    



if playerChoice == anwser():
    print("you won")
elif computerChoice == anwser():
    print("you lost")

 

Link to comment
Share on other sites

Link to post
Share on other sites

11 minutes ago, Celious said:

You might of saw me already with an earlier version of this program, I have now finished it but there is one bug.
Everything works fine but the "You won" and "You lost" statements, it seems to be kinda random. But here's the code:
INFORMATION: PYTHON 3.6.2
 


import time
from random import *

def computerChoice():
    Z = (randint(0,1))
    if Z == 1:
        return("HEADS")
    elif Z == 0:
        return ("TAILS")
    
    


def anwser():
    x = (randint(0,1))
    if x == 1:
        return("HEADS")
    elif x == 0:
        return ("TAILS")
    

playerChoice = input("Heads or Tails: ").upper()

if playerChoice == 'HEADS' or playerChoice == 'TAILS':
    print ("You chose:", playerChoice)
    time.sleep(0.2)
    print ("Computer chose:", computerChoice())
    time.sleep(0.2)
    print("The anwser was: ", anwser())
    



if playerChoice == anwser():
    print("you won")
elif computerChoice == anwser():
    print("you lost")

 

 

I should know this, but I don't... :(

 

Now I want to know this... ^^

 

When I run it, the computer can choose the same value as me, not sure if that is intended? ^^

Also, it's either won or nothingat all... :o 

Link to comment
Share on other sites

Link to post
Share on other sites

Do you ever store the computer's choice? It looks like you are just calling the function, rather than saving the result.

So when you check who has won the "computerChoice" variable is never set.

 

Looks like you need to store that choice too, should help a bit.

 

Also, since you aren't storing the answer, the answer will change potentially when you are checking it.

CPU: 6700k GPU: Zotac RTX 2070 S RAM: 16GB 3200MHz  SSD: 2x1TB M.2  Case: DAN Case A4

Link to comment
Share on other sites

Link to post
Share on other sites

 

Todays is you happy day: I fixed it all.

import time
from random import *

def computerChoice():
    Z = (randint(0,1))
    if Z == 1:
        return("HEADS")
    elif Z == 0:
        return ("TAILS")
    
    


def anwser():
    x = (randint(0,1))
    if x == 1:
        return("HEADS")
    elif x == 0:
        return ("TAILS")
a=1
while a
    playerChoice = input("Heads or Tails: ").upper()

    if playerChoice == 'HEADS' or playerChoice == 'TAILS':
        print ("You chose:", playerChoice)
        time.sleep(0.2)
        computerCh=computerChoice()        #change here
        print ("Computer chose:", computerCh)
        time.sleep(0.2)
        anws=anwser()                    #and here
         print("The anwser was: ", anws)
        a=0
    else:
          print("Please write heads or tails:") #never trust user input
    


if playerChoice == anws:
    print("you won")
else: #dont need as it only should be only one
    print("you lost")

 

Link to comment
Share on other sites

Link to post
Share on other sites

8 minutes ago, WiiManic said:

Do you ever store the computer's choice? It looks like you are just calling the function, rather than saving the result.

So when you check who has won the "computerChoice" variable is never set.

 

Looks like you need to store that choice too, should help a bit.

 

Also, since you aren't storing the answer, the answer will change potentially when you are checking it.

 

3 minutes ago, ignaloidas said:

 

Todays is you happy day: I fixed it all.


import time
from random import *

def computerChoice():
    Z = (randint(0,1))
    if Z == 1:
        return("HEADS")
    elif Z == 0:
        return ("TAILS")
    
    


def anwser():
    x = (randint(0,1))
    if x == 1:
        return("HEADS")
    elif x == 0:
        return ("TAILS")
a=1
while a
    playerChoice = input("Heads or Tails: ").upper()

    if playerChoice == 'HEADS' or playerChoice == 'TAILS':
        print ("You chose:", playerChoice)
        time.sleep(0.2)
        computerCh=computerChoice()        #change here
        print ("Computer chose:", computerCh)
        time.sleep(0.2)
        anws=anwser()                    #and here
         print("The anwser was: ", anws)
        a=0
    else:
          print("Please write heads or tails:") #never trust user input
    


if playerChoice == anws:
    print("you won")
else: #dont need as it only should be only one
    print("you lost")

 

Hm was 50% on my way there as well. :D

I actually figured this with storing the values.

 

Really need to get back to working on my skills...

Just starting with python and programming in general.

 

Though, technically speaking, there's probably a more "clean" way to do it.

 

Link to comment
Share on other sites

Link to post
Share on other sites

On 26/09/2017 at 10:24 PM, WiiManic said:

Do you ever store the computer's choice? It looks like you are just calling the function, rather than saving the result.

So when you check who has won the "computerChoice" variable is never set.

 

Looks like you need to store that choice too, should help a bit.

 

Also, since you aren't storing the answer, the answer will change potentially when you are checking it.

how do i store it?, btw i updated the code a bit
 

import time
from random import randint


    
    

#Randomiser
def alpha():
    value = (randint(0,1))
    if value == 1:
        return("HEADS")
    elif value == 0:
        return ("TAILS")
  


#asking for input
player1 = input("(Player 1) Heads or Tails?: ").upper()

player2 = input("(Player 2) Heads or Tails?: ").upper()


#printing result
print("The coin is flipping...")

time.sleep(0.3)

print("The coin landed on: ",alpha())
### I need a way to store this value ### 
#So i can use it here vv ## 
if player1 == alpha():
    print("testing")

 

Link to comment
Share on other sites

Link to post
Share on other sites

nvm i got it, i assigned a variable to the randomiser then print the variable

Link to comment
Share on other sites

Link to post
Share on other sites

There is a more elegant way of picking a random answer:

import random

print(random.choice(["heads", "tails"]))

 

Desktop: Intel i9-10850K (R9 3900X died 😢 )| MSI Z490 Tomahawk | RTX 2080 (borrowed from work) - MSI GTX 1080 | 64GB 3600MHz CL16 memory | Corsair H100i (NF-F12 fans) | Samsung 970 EVO 512GB | Intel 665p 2TB | Samsung 830 256GB| 3TB HDD | Corsair 450D | Corsair RM550x | MG279Q

Laptop: Surface Pro 7 (i5, 16GB RAM, 256GB SSD)

Console: PlayStation 4 Pro

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×