如何从 python 中的字符串中减去整数?
How to have integer subtract from a string in python?
我目前正在从头开发一个聊天机器人,作为 python class 简介的作业。在我的任务中,我需要至少有一个“数学组件”。我似乎无法找出如何从字符串中减去我的输入整数。
附件是一个屏幕截图,我的目标是让他们输入他们每周在家做饭的天数,然后自动从 7 中减去。
print('Hello! What is your name? ')
my_name = input ()
print('Nice to meet you ' + my_name)
print('So, ' + my_name + ' What is your favorite veggie?')
favorite_veggie = input ()
print('Thats nuts! ' +favorite_veggie + ' is mine too!')
print('How many days a week do you have cook at home? ')
day = input ()
print('So what do you do the other ' + ????? 'days?')
day = int(input())
print('So what do you do the other', 7-day, 'days?')
您正在寻找这个
day = input()
required_days = 7 - int(day)
print('So what do you do the other ' + str(required_days) + ' days?')
您可以将input
的结果转换为int
,然后做减法。另外 input("")
接受一个字符串作为参数,它会显示在提示区域的前面,这样可以使代码更好
my_name = input('Hello! What is your name? ')
print('Nice to meet you ' + my_name)
favorite_veggie = input('So, ' + my_name + ' What is your favorite veggie?')
print('Thats nuts! ' + favorite_veggie + ' is mine too!')
day = int(input('How many days a week do you have cook at home? '))
non_work_day = 7 - day
what_do = input('So what do you do the other ' + str(non_work_day) + 'days?')
我目前正在从头开发一个聊天机器人,作为 python class 简介的作业。在我的任务中,我需要至少有一个“数学组件”。我似乎无法找出如何从字符串中减去我的输入整数。
附件是一个屏幕截图,我的目标是让他们输入他们每周在家做饭的天数,然后自动从 7 中减去。
print('Hello! What is your name? ')
my_name = input ()
print('Nice to meet you ' + my_name)
print('So, ' + my_name + ' What is your favorite veggie?')
favorite_veggie = input ()
print('Thats nuts! ' +favorite_veggie + ' is mine too!')
print('How many days a week do you have cook at home? ')
day = input ()
print('So what do you do the other ' + ????? 'days?')
day = int(input())
print('So what do you do the other', 7-day, 'days?')
您正在寻找这个
day = input()
required_days = 7 - int(day)
print('So what do you do the other ' + str(required_days) + ' days?')
您可以将input
的结果转换为int
,然后做减法。另外 input("")
接受一个字符串作为参数,它会显示在提示区域的前面,这样可以使代码更好
my_name = input('Hello! What is your name? ')
print('Nice to meet you ' + my_name)
favorite_veggie = input('So, ' + my_name + ' What is your favorite veggie?')
print('Thats nuts! ' + favorite_veggie + ' is mine too!')
day = int(input('How many days a week do you have cook at home? '))
non_work_day = 7 - day
what_do = input('So what do you do the other ' + str(non_work_day) + 'days?')