ALERT!
Click here to register with a few steps and explore all our cool stuff we have to offer!

Jump to content



Photo

A problem I encountered whilst making a program. (Python) If you know what my mistake is, please help! Thanks :)


  • Please log in to reply
A problem I encountered whilst making a program. (Python) If you know what my mistake is, please help! Thanks :)

#1

CurtainCrab
CurtainCrab
    Offline
    -1
    Rep
    21
    Likes

    Amateur

Posts: 105
Threads: 17
Joined: Oct 30, 2017
Credits: 0
Six years registered
#1

Hello! Even though I haven't introduced myself yet, I'm CurtainCrab. So, I was making a quick program to organise tournaments on Python. I had just started, and when I made a few tests to check, I encountered a problem that I couldn't find the solution to on the internet, so I thought I'd ask you.

# Program to organize a tournament 
 
# A few variables
 
players = list()
playercount = (int(input("Amount of players : ")))
name = ""
 
# A loop that will assign a name to each player in the list (players)
 
looper = (playercount)
 
while looper != 0:
    name = str(input("name of a player : "))
    players.append(name)
    looper = looper - 1

 
And so, I tried the code above, and everything seemed to work well until I entered the Line 14 (name) and it gave me the following error message :
 

[background=#262626] [/background]Traceback (most recent call last):
  File "/Users/delmasfamily/Desktop/tournament.py", line 14, in <module>
    name = str(input("name : "))
  File "<string>", line 1, in <module>
NameError: name 'john' is not defined

Thanks a lot for your time everybody! Have a good day and I'll see you later. 


Edited by CurtainCrab, 30 October 2017 - 10:56 PM.

  • 0

#2

BoliBerrys
BoliBerrys
    Offline
    251
    Rep
    909
    Likes

    AIOSerial <3

Posts: 3007
Threads: 164
Joined: Apr 12, 2015
Credits: 33

Eight years registered
#2

I've learnt really basic of Python, so probs my answer is not correct.. But pretty sure to read input, the code is 

 

name = raw_input("Name of a player" )


  • 1

o9YHxIF.gif


#3

CurtainCrab
CurtainCrab
    Offline
    -1
    Rep
    21
    Likes

    Amateur

Posts: 105
Threads: 17
Joined: Oct 30, 2017
Credits: 0
Six years registered
#3

Wow! I considered that but I never thought it would have worked! Thanks! 

As you may have considered above, I'm probably even more of a beginner than you are. :I


  • 0

#4

MrR0GUE
MrR0GUE
    Offline
    0
    Rep
    2
    Likes

    Addicted

  • PipPipPipPipPip
Posts: 169
Threads: 0
Joined: Jan 28, 2016
Credits: 0
Eight years registered
#4

Your question was answered but I thought I would clarify a bit. To return the input as a string in Python 2 you use raw_input() and in python 3 you use input(). There's also an input() function in Python 2 which is equivalent to eval(input()) in Python 3, This will run your input as python code.


  • 1

#5

CurtainCrab
CurtainCrab
    Offline
    -1
    Rep
    21
    Likes

    Amateur

Posts: 105
Threads: 17
Joined: Oct 30, 2017
Credits: 0
Six years registered
#5

Oh wow! So I was just using the wrong version. Thanks for the clarification. I'll keep note of it if I ever change versions.

Thanks again.


  • 0


 Users browsing this thread: