这些选项的函数(Python)
在这种情况下,如何使用函数 我想做的是 1-允许用户输入要保存的数字 2-允许用户减少保存的号码 3-将使用户查看保存的号码 这是我当前的代码: 输入“1”时: 如果输入了“2”: 如果输入3:这些选项的函数(Python),python,Python,在这种情况下,如何使用函数 我想做的是 1-允许用户输入要保存的数字 2-允许用户减少保存的号码 3-将使用户查看保存的号码 这是我当前的代码: 输入“1”时: 如果输入了“2”: 如果输入3: 我不知道您到底期望什么,但至少可以将代码移动到函数中: def add: print 'you have:' add = raw_input('how many do you want to add:') prompt() def remove: print 'you
我不知道您到底期望什么,但至少可以将代码移动到函数中:
def add:
print 'you have:'
add = raw_input('how many do you want to add:')
prompt()
def remove:
print 'you have:'
remove = raw_input ('How many do you want to remove:')
prompt()
def total:
print 'Total:'
def prompt:
print '1. Add \n2. Remove \n3. View'
x = raw_input('ur choice:')
if x == '1':
add()
elif x == '2':
remove()
else:
total()
考虑将函数放在字典中删除IF:ELI::我要做的是,当1个用户允许输入一个数字来保存2时,用户可以删除或减少保存的数字3,这会让用户查看保存的数字,通过将这些代码转换成函数来实现。你们还并没有说它是否已经起作用了,请把问题弄清楚。
if x == '1':
print 'you have:'
add = raw_input('how many do you want to add:')
print '1. Add \n2. Remove \n3. View'
if x == '2':
print 'you have:'
remove = raw_input ('How many do you want to remove:')
print '1. Add \n2. Remove \n3. View'
if x == '3':
print 'Total:'
def add:
print 'you have:'
add = raw_input('how many do you want to add:')
prompt()
def remove:
print 'you have:'
remove = raw_input ('How many do you want to remove:')
prompt()
def total:
print 'Total:'
def prompt:
print '1. Add \n2. Remove \n3. View'
x = raw_input('ur choice:')
if x == '1':
add()
elif x == '2':
remove()
else:
total()