0

I am having trouble with assigning values to vars and then accessing the values. For example:

# offer users choice for how large of a song list they want to create
# in order to determine (roughly) how many songs to copy
print "\nHow much space should the random song list occupy?\n"
print "1. 100Mb"
print "2. 250Mb\n"

tSizeAns = raw_input()

if tSizeAns == 1:
    tSize = "100Mb"
elif tSizeAns == 2:
    tSize = "250Mb"
else:
    tSize = 100Mb    # in case user fails to enter either a 1 or 2

print "\nYou  want to create a random song list that is " + tSize + "."

Traceback returns:

Traceback (most recent call last):
  File "./ranSongList.py", line 87, in <module>
    print "\nYou  want to create a random song list that is " + tSize + "."
NameError: name 'tSize' is not defined

I have read up on python variables and they do not need to be declared so I am thinking they can be created and used on the fly, no? If so I am not quite sure what the traceback is trying to tell me.

By the way, it appears as though python does not offer 'case' capabilities, so if anyone has any suggestions how to better offer users lists from which to choose options and assign var values I would appreciate reading them. Eventually when time allows I will learn Tkinter and port to GUI.

2
  • 3
    As written I get a syntax error from the tSize = 100Mb line - it needs to be quoted like the others. Commented Dec 31, 2013 at 23:19
  • 3
    Fixing missing quotes, I can't see how tSize could not be defined. It is surely assigned in one of the three statements Commented Dec 31, 2013 at 23:28

6 Answers 6

1

Your if statements are checking for int values. raw_input returns a string. Change the following line:

tSizeAns = raw_input()

to

tSizeAns = int(raw_input())
Sign up to request clarification or add additional context in comments.

Comments

1

This should do it:

#!/usr/local/cpython-2.7/bin/python

# offer users choice for how large of a song list they want to create
# in order to determine (roughly) how many songs to copy
print "\nHow much space should the random song list occupy?\n"
print "1. 100Mb"
print "2. 250Mb\n"

tSizeAns = int(raw_input())

if tSizeAns == 1:
    tSize = "100Mb"
elif tSizeAns == 2:
    tSize = "250Mb"
else:
    tSize = "100Mb"    # in case user fails to enter either a 1 or 2

print "\nYou  want to create a random song list that is {}.".format(tSize)

BTW, in case you're open to moving to Python 3.x, the differences are slight:

#!/usr/local/cpython-3.3/bin/python

# offer users choice for how large of a song list they want to create
# in order to determine (roughly) how many songs to copy
print("\nHow much space should the random song list occupy?\n")
print("1. 100Mb")
print("2. 250Mb\n")

tSizeAns = int(input())

if tSizeAns == 1:
    tSize = "100Mb"
elif tSizeAns == 2:
    tSize = "250Mb"
else:
    tSize = "100Mb"    # in case user fails to enter either a 1 or 2

print("\nYou want to create a random song list that is {}.".format(tSize))

HTH

Comments

1

In addition to the missing quotes around 100Mb in the last else, you also want to quote the constants in your if-statements if tSizeAns == "1":, because raw_input returns a string, which in comparison with an integer will always return false.

However the missing quotes are not the reason for the particular error message, because it would result in an syntax error before execution. Please check your posted code. I cannot reproduce the error message.

Also if ... elif ... else in the way you use it is basically equivalent to a case or switch in other languages and is neither less readable nor much longer. It is fine to use here. One other way that might be a good idea to use if you just want to assign a value based on another value is a dictionary lookup:

tSize = {"1": "100Mb", "2": "200Mb"}[tSizeAns]

This however does only work as long as tSizeAns is guaranteed to be in the range of tSize. Otherwise you would have to either catch the KeyError exception or use a defaultdict:

lookup = {"1": "100Mb", "2": "200Mb"}
try:
    tSize = lookup[tSizeAns]
except KeyError:
    tSize = "100Mb"

or

from collections import defaultdict

[...]

lookup = defaultdict(lambda: "100Mb", {"1": "100Mb", "2": "200Mb"})
tSize = lookup[tSizeAns]

In your case I think these methods are not justified for two values. However you could use the dictionary to construct the initial output at the same time.

1 Comment

appreciate the suggestions, b/c I only listed two values for ease and clarity, when in fact there will be approximately ten choices to choose from. So the solutions you provide will surely prove useful for me top get familiar with. Thanks.
0

Initialize tSize to

tSize = "" 

before your if block to be safe. Also in your else case, put tSize in quotes so it is a string not an int. Also also you are comparing strings to ints.

Comments

0

I would approach it like this:

sizes = [100, 250]
print "How much space should the random song list occupy?"
print '\n'.join("{0}. {1}Mb".format(n, s)
                for n, s in enumerate(sizes, 1)) # present choices
choice = int(raw_input("Enter choice:")) # throws error if not int
size = sizes[0] # safe starting choice
if choice in range(2, len(sizes) + 1):
    size = sizes[choice - 1] # note index offset from choice
print "You  want to create a random song list that is {0}Mb.".format(size)

You could also loop until you get an acceptable answer and cover yourself in case of error:

choice = 0
while choice not in range(1, len(sizes) + 1): # loop
    try: # guard against error
        choice = int(raw_input(...))
    except ValueError: # couldn't make an int
        print "Please enter a number"
        choice = 0
size = sizes[choice - 1] # now definitely valid

1 Comment

thanks for the alternate approaches. They will be real useful to figure out and improve my understanding of python's syntax.
0

You forgot a few quotations:

# offer users choice for how large of a song list they want to create
# in order to determine (roughly) how many songs to copy
print "\nHow much space should the random song list occupy?\n"
print "1. 100Mb"
print "2. 250Mb\n"

tSizeAns = raw_input()

if tSizeAns == "1":
    tSize = "100Mb"
elif tSizeAns == "2":
    tSize = "250Mb"
else:
    tSize = "100Mb"    # in case user fails to enter either a 1 or 2

print "\nYou  want to create a random song list that is " + tSize + "."

2 Comments

funny enough, when I include quotes around the 'tSizeAns' vars as you suggest the problem recurs that the 'else' statement executes when a valid choice was entered by user. Do the quotes then alter the nature of the var? This is surely where I am getting messed up. In bash I used to run into problems with quoting and soon enough learned to quote or enclose my vars in curly bracket no matter what and that seemed to solve most of my var issues there. I will continue digging to learn more about properly quoting vars in python.
@nanker You shouldn't put quotes around vars. Having quotes around something causes Python to interpret it as text. so print("var") actually prints var whereas print(var) prints the value of a variable called var.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.