简体   繁体   中英

How to have integer subtract from a string in python?

I'm currently working on a chatbot from scratch as an assignment for my intro to python class. In my assignment I need to have at least one "mathematical component". I cant seem to find out how to have my input integer subtract from a string.

Attached is a screen shot, My goal is to have them input how many days a week they cook at home and have that subtract from 7 automatically.

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?')

You are looking for this

day = input()
required_days = 7 - int(day)
print('So what do you do the other ' + str(required_days) + ' days?')

You may convert the result of the input to int , then do the substraction. Also input("") accepts a string as parameter, that will be shown in front of the prompt area, that makes better code

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?')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM