Rock Paper Scissors Python


#1

Hello,
I'm trying to make a rock paper or scissors game.
But the when I added the winning part it doesn't run it.

import random
print('Hello User, Welcome to rock, paper, scissors')
print('What is your name?')
User = input()

print("Hello " + User + "")
print("Let's start " + User + "")
Attack = input("Rock Paper Scissors:")
from random import choice
cpu_choice = choice(('rock', 'paper', 'scissors'))
print(cpu_choice)
if cpu_choice == Attack:
    print('It is a tie!')
elif (Attack == 'rock'):
    if (computer == 'paper'):
        print('Cpu wins!')

elif (Attack == 'paper'):
    if (computer == 'scissors'):
        print('Cpu wins!')

elif (Attack == 'scissors'):
    if (computer == 'rock'):
        print('Cpu wins!')

elif (Attack == 'rock'):
    if (computer == 'scissors'):
        print( + User + "wins!")

elif (Attack == 'scissors'):
    if (computer == 'paper'):
        print( + User + "wins!")

elif (Attack == 'paper'):
    if (computer == 'rock'):
        print( + User + "wins!")

#2

Hi @didntcode,

Here, you create a variable named cpu_choice ...

cpu_choice = choice(('rock', 'paper', 'scissors'))

However, later in the program you have this ...

elif (Attack == 'rock'):
    if (computer == 'paper'):
        print('Cpu wins!')

The variable computer has not been defined. Should computer be changed to cpu_choice?

Variables are usually given names that begin with lowercase letters, and the following discussion modifies the names you used in order to follow that convention.

You need to consolidate the elif blocks. For example, instead of having two blocks with the following header ...

elif (attack == "rock"):

... combine them into one block as follows ...

elif (attack == "rock"):
    if (cpu_choice == "paper"):
        print("Cpu wins!")
    else:
        print(user + " wins!")

There are some other things you can do to neaten up the code. For example, you can prompt the user for a name, collect the input, and set the first letter of the name to uppercase all in one statement, as follows ...

user = input("What is your name? ").title()

You can also standardize the user's choice to lowercase by doing this ...

attack = input("rock, paper, or scissors: ").lower()

#3

I changed the user script but when I input the name it gives me an error at line six.

import random
print('Hello User, Welcome to rock, paper, scissors')
User = ''
User = input('What is your name?:').title

print("Hello" + User + "")
print("Let's start " + User + "")
Attack = input("Rock Paper Scissors:")
from random import choice
cpu_choice = choice(('rock', 'paper', 'scissors')).lower()
print(cpu_choice)
if cpu_choice == Attack:
    print('It is a tie!')
elif (Attack == 'rock'):
    if(cpu_choice == 'paper'):
        print('Cpu wins!')
    else:
        print(User + " wins!")

#4

this is the error

line 6, in <module>
    print("Hello" + User + "")
TypeError: must be str, not builtin_function_or_method

#5

Hi @didntcode,

What version of Python are you using to run the program? If you are using Python version 2, you should use raw_input to get user input instead of using the input function.

This needs parentheses after title ...

User = input('What is your name?:').title

#6

I'm using python 3.6


#7

OK, then the input function is what you should use.

The problem in the following line is that without parentheses at the end, you are assigning the title method itself to User instead of calling the title method and assigning the result to User.

User = input('What is your name?:').title

You should do this instead ...

User = input('What is your name?:').title()

The main organizational problem with your original code was the arrangement of the if and elif blocks.

The following is your code with a few adjustments made to it. The variable names have been revised to begin with lowercase letters, and the conditional blocks have been reorganized.

from random import choice
print("Hello user, Welcome to rock, paper, scissors!")
# Get user's name and set first letter to uppercase
user = input("What is your name? ").title()

print("Hello " + user + ".")
print("Let's start, " + user + ".")

# Get user's input and set it to all lowercase
attack = input("rock, paper, or scissors: ").lower()

# Get Cpu's choice
cpu_choice = choice(("rock", "paper", "scissors"))

# Announce Cpu's choice
print("Cpu chose: " + cpu_choice)

# Determine and announce winner
if cpu_choice == attack:
    print("It is a tie!")
elif (attack == "rock"):
    if (cpu_choice == "paper"):
        print("Cpu wins!")
    else:
        print(user + " wins!")
elif (attack == "paper"):
    if (cpu_choice == "scissors"):
        print("Cpu wins!")
    else:
        print(user + " wins!")
elif (attack == "scissors"):
    if (cpu_choice == "rock"):
        print("Cpu wins!")
    else:
        print(user + " wins!")

Try it out to see if it does what you intended, continue to revise it as necessary, and post new versions of your code if you have any questions about it.


#8

it worked!
I've got 1 question: What does the .lower() and .title() do.

and do you know a way to make an app or program with a design, because it looks boring in Idle.


#9

Here is the official documentation for those methods ...

-str.lower()
-str.title()

For that, there are many different options. If you would like to develop interactive web pages, start with HTML and CSS and then try Codecademy's various JavaScript and related courses or Codecademy's Ruby on Rails instructional material.

With the Django web framework, Python can be used to develop interactive web pages.

Also consider Tkinter and other GUIs for using Python to develop standalone applications.

Browse through material on these topics to find out whether any of it interests you.


#10

oh ok thanks for the help.


#11