How do add whitespace in between a word in python - python

Hi I am a newbiew learning python and i have a very simple question and can't seem to work it out.
I have built this little program and i was just wondering one thing.
print("Hello Sir !!!")
num1 = input("Enter a number: ")
num2 = input("Enter another Number: ")
result = float(num1) + float(num2)
print(result)
num3 = input("Enter your final Number: ")
result = float(num3) / (float(num1) + float(num2))
print("Your final total is:", result)
print("You are now finished")
print("Have an Amazing day!! ")
RESULT =
Hello Sir !!!
Enter a number: 50
Enter another Number: 50
100.0
Enter your final Number: 5
Your final total is: 0.05
You are now finished
Have an Amazing day!!
Process finished with exit code 0
If i wanted to write "Your final total is:0.05" or "Your final total is:
0.05"
How would i move it closer or further away?
Thank you for your help today

If you want to add more whitespaces, you can just added it inside the string. If you want a new line, you can use "\n" in your string, which indicate the start of a new line.
Check this link to find out more about escape characters:
https://www.tutorialspoint.com/escape-characters-in-python

You can do
print("Some string: " + variable) # Add a whitespace at the end of your string
if you have string with variable or
print(variable1 + ' ' + variable2)
If you need space between 2 variables or use \n if you need to make a newline

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 exactly do we make use of 'continue' in Python? [duplicate]

This question already has answers here:
How can I read inputs as numbers?
(10 answers)
Closed 4 months ago.
i=0
while (i==0):
n1=float(input("Enter the first number: \n"))
n2=float(input("Enter the second number: \n"))
a=str(input("Choose the operation: \n 1.) + \n 2.) - \n 3.) * \n 4.) / \n 5.) % \n"))
if(a==1):
print("The addition of the inputted numbers is ", sum(n1,n2),"\n")
elif(a==2):
print("The difference between the inputted numbers is ", diff(n1,n2),"\n")
elif(a==3):
print("The multiplication of the inputted numbers is ", product(n1,n2),"\n")
elif(a==4):
print("The division of the inputted numbers is ", div(n1,n2),"\n")
elif(a==5):
print("The modulus of the inputted numbers is ", modulus(n1,n2),"\n")
else:
print("Do you want to quit the calculator?")
b=int(input("Press 00 for 'Yes' and 11 for 'No'\n"))
if(b==00):
break
else:
print("Enter a valid option for the operation number.\nThe calculator is re-starting.")
continue
This is my python code. I have defined all the functions used here.
The output I am getting is:
When I inputted the correct operation number 3 at the place I have circled in the image, why is the output not giving me the final calculated answer? Why is it repeating the same loop again and again?
Please help me in this.
Hacker already provided you with all the changes that need to be done. You need to look at your integer vs string comparison, your while loop usage, and your Try-Except options.
To help you have a reference point, here's modified code for you to compare:
while True:
while True:
try:
n1=float(input("Enter the first number: \n"))
break
except:
print ('Enter a number. It can be integer or float')
while True:
try:
n2=float(input("Enter the second number: \n"))
break
except:
print ('Enter a number. It can be integer or float')
while True:
try:
a=int(input("Choose the operation: \n 1.) + \n 2.) - \n 3.) * \n 4.) / \n 5.) % \n"))
if 1 <= a <= 5:
break
else:
print ('Enter a number within range 1 and 5')
except:
print ('Not a number. Please enter an integer')
if (a==1):
print("The addition of the inputted numbers is ", (n1+n2),"\n")
elif (a==2):
print("The difference between the inputted numbers is ", (n1-n2),"\n")
elif (a==3):
print("The multiplication of the inputted numbers is ", (n1*n2),"\n")
elif (a==4):
print("The division of the inputted numbers is ", n1/n2,"\n")
elif (a==5):
print("The modulus of the inputted numbers is ", int(n1%n2),"\n")
print("Do you want to quit the calculator?")
while True:
try:
b=int(input("Press 00 for 'Yes' and 11 for 'No'\n"))
if b==0: break
elif b!= 11: print ('Enter a valid option: 00 or 11')
except:
print ('Not a number. Please enter an integer')
I am just going to summarize the comments and add some stuff.
The while loop will run forever if you do not break it because i is always going to be 0 because it is not changed inside of the loop.
As Thierry Lathuille pointed ot friendly a is defined as a stra str can never be equal to a number: you would have to do:
int(a)==1 or a==str(1)
Your input was 11 and as expected the calculator said that he is going to restart and did not break.
continue will restart the loop while break will stop it. As #chepner pointed out: if continue is the last possible statement then it doesn`t do anything.
Also pointed out by #chepner is that 00 is equal to 0

Python 2: Next input line not showing

I have tried to run one simple code for enter the user input and print that value like below :
num1 = input('Enter first number: ')
num2 = input('Enter second number: ')
sum = float(num1) + float(num2)
print('The sum of {0} and {1} is {2}'.format(num1, num2, sum))
Now when i run this code then its only provide the option to enter the first input like below:
Enter first number:
once i input the number in this line then nothing to happened and also not showing the next one line for the input.
Can you please let me know if i missed anything here? or any idea please share.

What is the Syntax error in this code that detects an even or odd number in python?

Here is the code that the syntax is on:
# Odd or Even?
print('Hello! What is your name?')
PlayerName = input()
print("Hello, " + PlayerName + "! Enter your number and I\'ll tell you if it\'s even or odd!")
PlayerNum = input()
Decimal = PlayerNum % 2
if Decimal == 0.5
print('Your number is odd!')
else
print('Your number is even!')
It gives syntax on line 7 (if Decimal == 0.5). Let me know if there are any other errors. I'm also using python 3. What's the problem?
Thanks
Your code should look like this:
print('Hello! What is your name?')
playerName = input()
print("Hello, " + playerName + "! Enter your number and I\'ll tell you if it\'s even or odd!")
playerNum = int(input())
if playerNum % 2 == 0:
print('Your number is even!')
else:
print('Your number is odd!')
You need to insert a colon after if and else conditions.
The input is read as a string, you need to convert it to int or float
You need to indent your code (I recommend 4 spaces)

Taking apart strings in Python

So I am making a quick calculator script in Python, and I need to take apart a short string. The program first displays a short welcome message, then a prompt asks What they want to do calculate, and shows them the correct format. The functionality is there to do the calculation but unfortunately, I can not get the string dissecting bit working.
Here's my code
print ("--------------------------------------")
print (" ")
print ("Advanced Quick Calculator")
print ("By Max M, licenced under GPLv3")
print (" ")
print ("--------------------------------------")
statement = raw_input ("Please enter your mathematical statement [3 3 plus minus times divide]: ")
strnum1 = statement[:1]
print ("strnum1 : " + strnum1)
#num1 = int (strnum1)
strnum2 = statement[:4]
print ("strnum2 : " + strnum2)
#num2 = int (strnum2)
operation = statement[5:11]
print ("operation : " + operation)
#if operation == "+":
# ans = num1 + num2
#if operation == "-":
# ans = num1 - num2
#if operation == "*":
# ans = num1 * num2
#if operation == "/":
# ans = num1 / num2
#print ("The answer is : "), ans
This looks like a job for regular expressions:
>>> import re
>>> match = re.search(r'(\d+)\s*([+*/-])\s*(\d+)', '42 + 7')
>>> match.group(1) # <-- num1
'42'
>>> match.group(2) # <-- operation
'+'
>>> match.group(3) # <-- num2
'7'
Slicing the input like you're currently doing is probably not a good idea as it greatly restricts the allowed formats. For instance, what if the user accidentally precedes his input with a couple of spaces? Regular expressions can handle such cases well.
I'm not going to do your homework for you, but I will point you to the answer to your question (hint: it looks like you're trying to split on spaces instead of comma's like in the link, so adjust the code accordingly).
How to read formatted input in python?

Categories

Resources