Python Forum

Full Version: Why am I getting this TypeError Exception?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Please see the attached photo. This is my first Python program. It runs fine and the code appears to be the same as the instructors. Yet VSCode is telling me I have an error and this error is not making sense to me. Can anyone please help me to understand what is going on here? Thanks.

Error is as follows and I have attached the screenshot to this post:

Exception has occurred: TypeError
check_win() missing 2 required positional arguments: 'player' and 'computer'
File "/Users/matthew/anaconda3/envs/pure_python/rock_paper_scissors.py", line 21, in <module>
results = check_win()
^^^^^^^^^^^
TypeError: check_win() missing 2 required positional arguments: 'player' and 'computer'
Please post the error text and the program code. The image link is already broken. Even if the link wasn't broken always post code and error messages as text. I cannot run your program if it is a screenshot. I can copy the program if it is text.
Full program:

import random

moves = ["rock", "paper", "scissors"]

def get_choices():
    player_choice = input("Enter a choice of rock, paper, or scissors: ")
    computer_choice = random.choice(moves)

    choices = {"player": player_choice, "computer": computer_choice}

    return choices 

def check_win(player, computer):
    print("You chose " + player + " and the computer chose " + computer)
    if player == computer:  
        return "It's a tie"
    
    elif player == "rock":
        if computer == "scissors":
            return "Rock beats scissors"
        else:
            return "Paper beats rock"
        
    elif player == "paper":
        if computer == "rock":
            return "Paper beasts rock"
        else:
            return "Scissors beats paper"
        
    elif player == "scissors":
        if computer == "paper":
            return "Scissors beats paper"
        else:
            return "Rocks beats scissors"
    
    
choices = get_choices()

results = check_win(choices["player"],choices["computer"])

print(results)
I don't get an error when I run your code. What is the error message you see?
(Jul-05-2023, 03:29 PM)deanhystad Wrote: [ -> ]I don't get an error when I run your code. What is the error message you see?

Thanks for the reply. My code runs fine and I don't understand why there should be any error message. But I get this error message:

Exception has occurred: TypeError
check_win() missing 2 required positional arguments: 'player' and 'computer'
File "/Users/matthew/anaconda3/envs/pure_python/rock_paper_scissors.py", line 21, in <module>
results = check_win()
^^^^^^^^^^^
TypeError: check_win() missing 2 required positional arguments: 'player' and 'computer'
I have also a screenshot attached to the first post of this.

I am trying to understand why I would be getting this error message.
The code you are running is not the code that you posted.

The code you are running does this:
results = check_win()
The code that you posted does this:
results = check_win(choices["player"],choices["computer"])
How do you run your program in VSCode?
(Jul-05-2023, 04:10 PM)deanhystad Wrote: [ -> ]The code you are running is not the code that you posted.

The code you are running does this:
results = check_win()
The code that you posted does this:
results = check_win(choices["player"],choices["computer"])
How do you run your program in VSCode?

I just press the play button at the top of VSCode.

Thanks for that feedback.

I cut and paste the code from the current program to a newly create .py file with a different name. Now that code runs fine without any errors in VSCode.

But now I have this confusing state where I have two windows open is VSCode with the exact same code yet one is returning an error and the other is not. It's as if VSCode is holding onto an error from an earlier time when I ran the program. But even if I close that original file, and reopen it in VSCode, it keeps regenerating that phantom error for the original file.
I can see your screen dump now.

It is odd seeing VSCode running without selecting a folder where it looks for your code. Then I noticed where your program file is saved.

I think your problem might be that your program is saved under anaconda3/envs/pure_python/. You should not be putting files in your virtual environement (other than installed packages). You should save your python programs in a different folder that is not part of the python distribution. I have never saved my program files in the environment, but this might be what is causing your problem. I don't know. It is just odd. Remember that VSCode runs the python file, not the text appearing in the editor window (unless you are using the run python code option for the Run button). If there is something preventing VSCode from updating the file, you will not be running the code displayed in the editor after an edit.

Create a folder where you want to save your python programs while learning. Save your "rock_paper_scissors.py" program in this folder. Do the same with your "test.py" file and any other files you have opened in VSCode. Now close all those editor windows and select File->Open Folder and select the folder you just created.
If I use VSCode, then, after a change, I press notoriously CTRL+S to save the changes. Just pressing the play button, starts the unmodified file.

PyCharm is different. All changes are written directly to disk immediately, so you don't have to save every time by yourself. In addition, the support for TypeHints is better. In VSCode you have to add this functionality with Extensions.