在下面的代码中为什么x
和y
字符串而不是整数?网上的所有内容都说要使用raw_input()
,但是我读input()
了raw_input()
在Python 3.x中重命名的Stack Overflow(在一个不处理整数输入的线程上).
play = True
while play:
x = input("Enter a number: ")
y = input("Enter a number: ")
print(x + y)
print(x - y)
print(x * y)
print(x / y)
print(x % y)
if input("Play again? ") == "no":
play = False
Run Code Online (Sandbox Code Playgroud) 我正在使用以下课程轻松存储我的歌曲数据.
class Song:
"""The class to store the details of each song"""
attsToStore=('Name', 'Artist', 'Album', 'Genre', 'Location')
def __init__(self):
for att in self.attsToStore:
exec 'self.%s=None'%(att.lower()) in locals()
def setDetail(self, key, val):
if key in self.attsToStore:
exec 'self.%s=val'%(key.lower()) in locals()
Run Code Online (Sandbox Code Playgroud)
我觉得这比写出一个if/else
块更具可扩展性.但是,eval
似乎被认为是一种不良做法并且使用起来不安全.如果是这样,任何人都可以向我解释为什么并告诉我一个更好的方法来定义上面的类?