Invalid string input in pycharm

Asked 2 years ago, Updated 2 years ago, 26 views

import random
def shuff():
    num = ['2', '3', '4', '5', '6', '7', '8', '9', 'T', 'J', 'Q', 'K', 'A']
    suit = ['s', 'd', 'h', 'c']
    deck = []
    for i in suit:
        for j in num:
            deck.append(j + i)

    random.shuffle(deck)

    return deck




while True:
    inp = str(input("Wanna deal?:"))
    if inp == 'y':
        print(shuff())
    else:
        continue

It works fine if you put it in the web interpreter If you put it in pycharm,

C:\Python27\python.exe C:/Users/Alpaca/PycharmProjects/test/n.py Wanna deal?:y Traceback (most recent call last): File "C:/Users/Alpaca/PycharmProjects/test/n.py", line 18, in inp = str(input("Wanna deal?:")) File "", line 1, in NameError: name 'y' is not defined

Process finished with exit code 1

There's an error like this. What's the problem?

python

2022-09-22 16:58

1 Answers

According to the log, you're using Python 2.7 version in PyCham Maybe it's because the web interpreter is python3 and pycharm is python2.

In python3, input() is a function that receives standard input.

In Python 2, input() is a function that receives python expression and runs. For example, if you run the code now and enter an expression such as 3 or 1+2 instead of a character such as y, the error will not occur.

Here's how to fix it: Please select one depending on the interpreter you want to use

Change the input function to raw_input in the code

inp = str(raw_input("Wanna deal?:"))

You can change it like this

You need to change the interpreter from 2 to 3 in pycharm setting.

Settings - Project: {Project Name} - Project Interpreter

If you go into , you can change the interpreter settings~


2022-09-22 16:58

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.