I'm using VSCode and clicking the "play" button at the top of the screen to test the code. Maybe it is just a weird VSCode issue, but if so, I don't really know where else to test my code to see if it works correctly. As you can tell, I'm quite new.
I'm running a basic RPS game on python, and receiving this error every other run, meanwhile the other runs are acting normally:
user = int(input("What will you choose? Press 0 for Rock, Press 1 for Paper, Press 2 for Scissors, or Press 3 to quit.\n"))
ValueError: invalid literal for int() with base 10: 'python3 -u "/Users/jferb/Python/RPS.py"'
any idea why this would happen? Here is my code:
import random
import sys
print ("Welcome to the game!")
while True:
rock = '''
______
---' ____)
( ____)
(_____)
(____)
---._(___)
'''
paper = '''
_______
---' _____)
______)
_______)
_______)
---.__________)
'''
scissors = '''
_______
---' ____)____
________)
____________)
(____)
---.__(___)
'''
rps = [rock, paper, scissors]
user = int(input("What will you choose? Press 0 for Rock, Press 1 for Paper, Press 2 for Scissors, or Press 3 to quit.\n"))
if user > 3 or user < 0:
print("You typed an invalid number, you lose!")
continue
if user == 3:
print("Goodbye!")
break
print(rps[user])
print("\nComputer Chose:")
comp = random.choice(rps)
print(comp)
if user == 0 and comp == (rps[1]):
print("You lose!")
elif user == 1 and comp == (rps[2]):
print("You lose!")
elif user == 1 and comp == (rps[0]):
print("You win!")
elif user == 2 and comp == (rps[1]):
print("You win!")
elif user == 2 and comp == (rps[0]):
print("You lose!")
elif user == 0 and comp == (rps[2]):
print("You win!")
else:
print("It's a tie! Play again!")
CodePudding user response:
I tried your code out, it worked fine. Trying to run it by clicking the run button blocked stdin completely for me. That's standard Code Runner extension behaviour though.
It seems that whatever runner you're using is handling stdin to run the script as you are getting the run string passed to stdin.
I've done a bit of testing. Are you using the Code Runner extension? If you are, just clicking the run button will use "Run Code" and stdin is not properly piped through. You only get the stdout from an Output tab. The only strange thing here is how you're getting any inputs through if this is the case. I can't say if there's some configuration problem or clashing of extensions.
There's a smaller arrow next to the run button which you can use to "Run Python File" which should work fine. (If you have the python extension added.)
The alternative is to instead open the folder in a terminal, integrated or otherwise, and run python <scriptname>.py
.
I personally prefer just using the terminal because that's how most people will be running your terminal app.
CodePudding user response:
This is happening because of the line:
user = int(input("What will you choose? Press 0 for Rock, Press 1 for Paper, Press 2 for Scissors, or Press 3 to quit.\n"))
If you don't enter an integer (1,2 or 3), the error shows up because you're trying to convert a 'string' or a float into an integer.