我在为vars分配值然后访问值时遇到问题。例如:
# 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 (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
我已经读过python变量并且它们不需要声明,所以我认为它们可以在运行中创建和使用,不是吗?如果是这样,我不太确定追溯试图告诉我什么。
顺便说一下,似乎python不提供'case'功能,所以如果有人有任何建议如何更好地提供用户列表从中选择选项并分配var值我会很感激阅读它们。最终,当时间允许时,我将学习Tkinter并将其移植到GUI。
答案 0 :(得分:2)
除了上一个100Mb
中else
周围缺少引号外,您还要在if语句if tSizeAns == "1":
中引用常量,因为raw_input
返回一个string,与整数相比,总是返回false。
但是,缺少的引号不是特定错误消息的原因,因为它会在执行之前导致语法错误。请检查您发布的代码。我无法重现错误消息。
使用它的方式if ... elif ... else
基本上等同于其他语言中的case
或switch
,并且既不易读也不长。在这里使用很好。如果您只想基于另一个值分配值,可能可能的另一种方式是字典查找:
tSize = {"1": "100Mb", "2": "200Mb"}[tSizeAns]
但只有tSizeAns
保证在tSize
范围内时才会有效。否则,您必须捕获KeyError
异常或使用defaultdict:
lookup = {"1": "100Mb", "2": "200Mb"}
try:
tSize = lookup[tSizeAns]
except KeyError:
tSize = "100Mb"
或
from collections import defaultdict
[...]
lookup = defaultdict(lambda: "100Mb", {"1": "100Mb", "2": "200Mb"})
tSize = lookup[tSizeAns]
在您的情况下,我认为这些方法不适用于两个值。但是,您可以使用字典同时构造初始输出。
答案 1 :(得分:1)
您的if语句正在检查int值。 raw_input
返回一个字符串。更改以下行:
tSizeAns = raw_input()
到
tSizeAns = int(raw_input())
答案 2 :(得分:1)
这应该这样做:
#!/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)
顺便说一句,如果你愿意转向Python 3.x,那么差异很小:
#!/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
答案 3 :(得分:0)
将tSize初始化为
tSize = ""
在你的if块之前是安全的。另外在你的情况下,将tSize放在引号中,这样它就是一个字符串而不是一个int。您还要将字符串与整数进行比较。
答案 4 :(得分:0)
我会这样做:
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)
你也可以循环,直到你得到一个可接受的答案,并在出现错误时掩盖自己:
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
答案 5 :(得分:0)
你忘记了一些引用:
# 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 + "."