我希望jobber()
执行以下操作:如果用户输入"谈话"它运行chat()
和"计算" calculator()
。
这是我的第一个项目,我是全新的,所以感谢您的帮助。
import sys
import time
def chat():
print('Hello there. What is your name?')
name = sys.stdin.readline()
print('Your name is %s...interesting. My name is Chat I was created to have a small planned out conversation when you call me :)' % (name))
time.sleep(2)
print('How is the weather in your town today?')
weather = sys.stdin.readline()
time.sleep(1.5)
print('Your weather is %s? The weather in my town is quite sunny.' % (weather))
time.sleep(2)
print('This is all the conversation I can make at the moment. Please be patient while my creator trys to update me.')
def calculator():
print('How much money do you have?')
money = int(sys.stdin.readline())
print('How much are you paid?')
job = int(sys.stdin.readline())
print('How much do you usually spend?')
spent = int(sys.stdin.readline())
for week in range(0,30):
money = money + job - spent
print('Week %s. Money %s' % (week,money))
def jobber():
print('Type talk for a short chat. Type calc for a money calculator.')
choice = int(sys.stdin.readline())
if choice == 1:
chat()
if choice == 2:
calculator()
答案 0 :(得分:0)
为了收集用户输入,您最好使用input()函数。
答案 1 :(得分:0)
使用input()
并转换为lowercase
def jobber():
choice = input('Type talk for a short chat. Type calc for a money calculator.').lower()
if choice == "talk":
chat()
if choice == "calc":
calculator()
答案 2 :(得分:0)
您应该使用input()
内置函数,例如在money
函数中设置calculate
变量应该只是:
money = int(input('How much money do you have?'))
由于您只使用choice
变量来确定要执行的函数,因此您甚至可以考虑将输入放在if语句中:
if input('Type talk or chat.') == 'calc':
calculator()
else:
chat()
python tutorial是如何使用if语句和其他控制流工具的好指南。