How to have integer subtract from a string in python? - 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?')

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

day = int(input())
print('So what do you do the other', 7-day, '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?')

Related

How do I add the sum of two inputs?

Hello everyone I am very new to Python I am taking a beginner course at my college, and I am stuck on one portion of my project. Basically the goal of my code is to produce the output so that when I enter any age and name the result is supposed to find the sum of the age and number of letters in the entered name. So far this what I have typed up.
print('What is your name?')
myName = input()
print('What is your age?')
myAge = input()
sum = myAge + str(len(myName))
print(myName + ', if we add the number of letters in your name to your age then you will be ' + sum + ' in ' + str(len(myName)) + ' years.')
When I run the script all I get is the age+length of name which gives me a combination and not a sum. example 21 + 4 = 214.
Basically my issue with this is that I don't understand how to find the sum of both inputs so that I get result of adding age and the length of letters in a name. The last portion that I am trying write should in other words be this "Name, if we add the number of letters in your name to your age then you will be # in # years."
If anyone can explain to me how I can accomplish this then I would greatly appreciate it I have spent hours looking into this issue but can't figure it out.
When you use input() it returns a string.
So when setting it to sum, you end up with "21" + "4" (which will result in a string) -> "214"
Try converting myAge to an int like so
sum = int(myAge) + len(myName)
This will make it where you add 21 + 4 (now both numbers), so you'll get 25
Result of "input()" always is a string. You need to convert your input to integer using:
myAge = int(input('What is your age?'))
During the sum of the two variables should be converted to integers or decimals
print('What is your name?')
myName = input()
print('What is your age?')
myAge = input()
sum = float(myAge) + float((len(myName)))
print(myName + ', if we add the number of letters in your name to your age then you will be ' + str(sum) + ' in ' + str(len(myName)) + ' years.')

How to make a bill by Python

I'm a freshman, and I want to make a bill that write out what I bought, quantity and how much total cash it cost.
customer = str(input("Customer's name: "))
print ("Welcome to our store")
print ("This is our products")
print ("orange 0.5$)
print ("Apple 1$)
print ("Grape 0.5$)
print ("Banana 0.5$)
chooseproducts= int(input("What do you want to buy: "))
For output example. Can you guys help me please.
BILL
Orange 5 2.5$
Apple 3 3$
Grape 2 1$
Total: 10 6.5$
First, your str(input(customer's name: )) code needs to be changed. input() calls need a string as the question, and also the apostrophe has Python think that it is the start of a string.
Also, when you are printing the costs, you need another closing quotation mark. When you are asking the user about what they want to buy, I suppose you want them to input the name of the fruit. Since you have the int() in the front, Python cannot turn a string like "orange" or "grape" into an integer. When people want to buy multiple things, however, they might only put one thing in.
I suppose that this isn't all your code, and you have the part where you calculate the cost. If I were to write this code, I would write it like the following:
customer = str(input("Customer's Name:"))
print ("Welcome to our store")
print ("This is our products")
print ("Orange 0.5$")
print ("Apple 1$")
print ("Grape 0.5$")
print ("Banana 0.5$")
prices = {"orange":0.5,"apple":1,"grape":0.5,"banana":0.5}# I added this so we can access the prices later on
#chooseproducts= int(input("What do you want to buy: "))
# The code below this is what I added to calculate the cost.
productnum = input("How many things do you want to buy: ") # This is for finding the number of different fruits the buyer wants
while not productnum.isdigit():
productnum = input("How many different products do you want to buy: ")
productnum = int(productnum)
totalprice = 0
totalfruit = 0
print(' BILL')
for i in range(productnum):
chosenproduct = input("What do you want to buy: ").lower()
while not chosenproduct in ['orange','apple','banana','grape']:
chosenproduct = input("What do you want to buy: ").lower()
fruitnum = input("How many of that do you want to buy: ")
while not fruitnum.isdigit():
fruitnum = input("How many of that do you want to buy: ")
fruitnum = int(fruitnum)
totalfruit += fruitnum
price = fruitnum * prices[chosenproduct]
totalprice += price
startspaces = ' ' * (11 - len(chosenproduct))
endspaces = ' ' * (5 - len(str(fruitnum)))
print(chosenproduct.capitalize() + startspaces + str(fruitnum) + endspaces + str(price) + '$')
print('Total: ' + str(totalfruit) + ' ' * (5 - len(str(totalprice))) + str(totalprice) + '$')
Please make sure you understand the code before copying it, thanks!

Method gives diffrent output with loop [duplicate]

If I enter any value less than 24, it does print the "You will be old..." statement. If I enter any value greater than 24 (ONLY up to 99), it prints the "you are old" statement.
The problem is if you enter a value of 100 or greater, it prints the "You will be old before you know it." statement.
print ('What is your name?')
myName = input ()
print ('Hello, ' + myName)
print ('How old are you?, ' + myName)
myAge = input ()
if myAge > ('24'):
print('You are old, ' + myName)
else:
print('You will be old before you know it.')
You're testing a string value myAge against another string value '24', as opposed to integer values.
if myAge > ('24'):
print('You are old, ' + myName)
Should be
if int(myAge) > 24:
print('You are old, {}'.format(myName))
In Python, you can greater-than / less-than against strings, but it doesn't work how you might think. So if you want to test the value of the integer representation of the string, use int(the_string)
>>> "2" > "1"
True
>>> "02" > "1"
False
>>> int("02") > int("1")
True
You may have also noticed that I changed print('You are old, ' + myName) to print('You are old, {}'.format(myName)) -- You should become accustomed to this style of string formatting, as opposed to doing string concatenation with + -- You can read more about it in the docs. But it really doesn't have anything to do with your core problem.
The string '100' is indeed less than the string '24', because '1' is "alphabetically" smaller than '2'. You need to compare numbers.
my_age = int(input())
if my_age > 24:
print ('What is your name?')
myName = input ()
print ('Hello, ' + myName)
print ('How old are you?, ' + myName)
myAge = input ()
if int(myAge) > 24:
print('You are old, ' + myName)
else:
print('You will be old before you know it.')
Just a small thing about your code. You should convert the input from myAge to an integer (int) (number) and then compare that number to the number 24.;
Also, you should usually not add strings together as it is consider non-pythonic and it slow. Try something like print ('Hello, %s' % myName) instead of print ('Hello, ' + myName).
Python Strings Tutorial
Use int(myAge). I always use raw_input and also, you dont have to print your questions. Instead put the question in with your raw_inputs like so:
myName = raw_input("Whats your name?")
print ('Hello, ' + myName)
myAge = raw_input('How old are you?, ' + myName)
if int(myAge) > ('24'):
print('You are old, ' + myName)
else:
print('You will be old before you know it.')

3 is bigger than 10 according to python [duplicate]

If I enter any value less than 24, it does print the "You will be old..." statement. If I enter any value greater than 24 (ONLY up to 99), it prints the "you are old" statement.
The problem is if you enter a value of 100 or greater, it prints the "You will be old before you know it." statement.
print ('What is your name?')
myName = input ()
print ('Hello, ' + myName)
print ('How old are you?, ' + myName)
myAge = input ()
if myAge > ('24'):
print('You are old, ' + myName)
else:
print('You will be old before you know it.')
You're testing a string value myAge against another string value '24', as opposed to integer values.
if myAge > ('24'):
print('You are old, ' + myName)
Should be
if int(myAge) > 24:
print('You are old, {}'.format(myName))
In Python, you can greater-than / less-than against strings, but it doesn't work how you might think. So if you want to test the value of the integer representation of the string, use int(the_string)
>>> "2" > "1"
True
>>> "02" > "1"
False
>>> int("02") > int("1")
True
You may have also noticed that I changed print('You are old, ' + myName) to print('You are old, {}'.format(myName)) -- You should become accustomed to this style of string formatting, as opposed to doing string concatenation with + -- You can read more about it in the docs. But it really doesn't have anything to do with your core problem.
The string '100' is indeed less than the string '24', because '1' is "alphabetically" smaller than '2'. You need to compare numbers.
my_age = int(input())
if my_age > 24:
print ('What is your name?')
myName = input ()
print ('Hello, ' + myName)
print ('How old are you?, ' + myName)
myAge = input ()
if int(myAge) > 24:
print('You are old, ' + myName)
else:
print('You will be old before you know it.')
Just a small thing about your code. You should convert the input from myAge to an integer (int) (number) and then compare that number to the number 24.;
Also, you should usually not add strings together as it is consider non-pythonic and it slow. Try something like print ('Hello, %s' % myName) instead of print ('Hello, ' + myName).
Python Strings Tutorial
Use int(myAge). I always use raw_input and also, you dont have to print your questions. Instead put the question in with your raw_inputs like so:
myName = raw_input("Whats your name?")
print ('Hello, ' + myName)
myAge = raw_input('How old are you?, ' + myName)
if int(myAge) > ('24'):
print('You are old, ' + myName)
else:
print('You will be old before you know it.')

How to make guessing the number game(high, low hints) with cheating allowed (Ulam's Game) in python?

I made a game of guessing the number where computer choose a secret number between 1 to 100 and i try to guess that number. On each guess, computer gives hint that whether it is lower or higher than his secret number by saying "too low" or "too high". But i want that sometimes computer will cheat ( for example in place of saying "too low" it will say "too high"). But whenever he tells a lie, in very next guess he must have to tell the truth. He must not to be allowed to lie in two consecutive guesses. Though he can lie alternatively. He may tell the truth without any limitation, consecutively and anytime. In below code, computer randomly say "too low" or "too high". By doing so he is sometimes lying consecutively more than once. Please fix the code so that it doesn't lie in two consecutive guesses.
import random
myName = input('What is your name?~ ')
print('Well, ' + myName + '! I am thinking of a number from 1 to 100')
number = random.randint(1, 100)
guessesTaken = 0
result = ['low', 'high']
while guessesTaken < 20:
guess = int(input(str(guessesTaken + 1) + '-Take a guess!~ '))
guessesTaken += 1
if guess < number:
print('Your guess is too ' + random.choice(result) + '!')
if guess > number:
print('Your guess is too ' + random.choice(result) + '!')
if guess == number:
break
if guess == number:
print('Good job, ' + myName + '! You guess my number in ' + str(guessesTaken) + ' guesses')
else:
print('Nope! The number I was thinking of was ' + str(number))
Here is how I solved the problem.
We define a variable can_lie. It tells if the computer is allowed to lie, it's True if the last thing he told was the truth.
Then if we are allowed to lie, and only one time over two, we lie.
After this we are not allowed to do it for the next turn, so can_lie become False.
Then we say the opposite of what we should have said, because we are lying.
If we are telling the truth, then can_lie become True, so we are allowed to tell whatever we want the next turn.
Then we tell what we are supposed to say.
import random
myName = input('What is your name?~ ')
print('Well, ' + myName + '! I am thinking of a number from 1 to 100')
number = random.randint(1, 100)
guessesTaken = 0
result = ['low', 'high']
can_lie = True
while guessesTaken < 20:
guess = int(input(str(guessesTaken + 1) + '-Take a guess!~ '))
guessesTaken += 1
if can_lie and random.choice((True, False)):
#Here we lie
can_lie = False
if guess > number:
print('Your guess is too low !')
if guess < number:
print('Your guess is too high !')
else:
can_lie = True
if guess < number:
print('Your guess is too low !')
if guess > number:
print('Your guess is too high !')
if guess == number:
break
if guess == number:
print('Good job, ' + myName + '! You guess my number in ' + str(guessesTaken) + ' guesses')
else:
print('Nope! The number I was thinking of was ' + str(number))
This code lies randomly, but never twice in a row.
import random
myName = input('What is your name?~ ')
print('Well, ' + myName + '! I am thinking of a number from 1 to 100')
number = random.randint(1, 100)
guessesTaken = 0
result = ['low', 'high']
previous_prediction = True
while guessesTaken < 20:
guess = int(input(str(guessesTaken + 1) + '-Take a guess!~ '))
guessesTaken += 1
if guess == number:
break
if previous_prediction:
low_high = random.choice(result)
else: # Make random choice only if told truth in previous choice
low_high = 'low' if guess < number else 'high'
print('Your guess is too ' + low_high + '!')
#If there is a lie between actual result and told result, mark the prediction as False, such that next time, only truth is provided
if not (( (guess < number) and low_high=='low') or ( (guess > number) and low_high=='high')):
previous_prediction = False
else:
previous_prediction = True
if guess == number:
print('Good job, ' + myName + '! You guess my number in ' + str(guessesTaken) + ' guesses')
else:
print('Nope! The number I was thinking of was ' + str(number))
However, you should have tried a bit more to solve it.

Categories

Resources