Home > Net >  How can I ask for user input in Python?
How can I ask for user input in Python?

Time:11-10

So to start coding I have to ask for a user input. They have to place 4 cards like so

A-S,A-H,A-C,A-D

Then I would create a list from their input. It should take the 2nd element then the 4th element from their input

4cards = input()
List1 = []
List1.append(4cards[1], 4cards[3])
List1pair = ', '.join(P2)
print 'List1 cards: {0}.format(List1pair)'

This should or I hope it prints this (I haven't check yet)

List1 cards: A-H,A-D

But my code returns with a syntax error "SyntaxError: invalid syntax"

4cards = input()

How do I resolve this?

CodePudding user response:

"SyntaxError: invalid syntax" shows because variable name can not start with numbers. you can use four_cards instead of 4cards.

four_cards = input()

CodePudding user response:

You can't use number first 4cards. Use variable I.E _4cards

_4cards = input()
List1 = []
List1.append(_4cards[1], _4cards[3])
List1pair = ', '.join(P2)
print ('List1 cards: {0}.format(List1pair)')

CodePudding user response:

Hi The reson you are getting error because of the name of variable 4cards we cant use this as a variable name it is starting with a number so you can use FourCards , Cards4 e.t.c

CodePudding user response:

variables can not start with numbers and in python3 print function comes with parentheses : print(statement)

  • Related