This question already has answers here:
Center-aligning text on console in Python
(3 answers)
Closed 9 years ago.
i'm trying to center the text of a string but i can't do it. I checked mutiple posts and tried what they said, i don't get an error but the text is still aligned to the left. here the code and the str i want to center
while True:
print ''
while True:
try:
userNum = float(raw_input('Enter the total of your bill: '))
if (userNum) > 0 and (userNum) != 0:
break
else:
print 'Oops! That was no valid number. Try again...'
You could use str.format. The ^80 format tells Python to make the string 80 characters long, and to center the argument:
print('{:^80}'.format('Oops! That was not a valid number. Try again...'))
yields
Oops! That was not a valid number. Try again...
take a look at this str.center
Related
This question already has answers here:
How can I read inputs as numbers?
(10 answers)
Closed 10 months ago.
n=[0]*365
s=0
s1=1
s2=2
s3=3
s4=4
s5=5
s6=6
for i in range(len(n)):
n[i]=4+1
s=s+7
s1=s1+7
s2=s2+7
s3=s3+7
s4=s4+7
s5=s5+7
s6=s6+7
if n[i]==s:
n[i]=0
elif n[i]==s1:
n[i]=1
elif n[i]==s2:
n[i]=2
elif n[i]==s3:
n[i]=3
elif n[i]==s4:
n[i]=4
elif n[i]==s5:
n[i]=5
elif n[i]==s6:
n[i]=6
Hey everyone here is my code i need to imput which number in row in array it is and it needs print out the number like if i input 300 it needs to output the 300th number in the list
I have tried using print(n[input()] but that obvioslly didnt work can you please help me
It is because input returns a string. The following should work:
print(n[int(input())])
However, note that this will fail if you input something else than a number as python will fail in the int conversion.
This question already has answers here:
How do I check if a string represents a number (float or int)?
(39 answers)
How can I check if string input is a number?
(30 answers)
Closed 1 year ago.
I'm trying to make a function that count the numbers of characters in a string and detect when an integer is typed, the condition is use "if" function. I'd expect that if I type any integer like"4464468" instead a string, the program displayed: "Sorry, you typed an integer". But, instead, counts the total number and displayed "The word you type has 7 characters".
My code is next:
def string_lenght(mystring):
return len(mystring)`
#Main Program
mystring = (input("Type a word: "))
if type(mystring) == int or type(mystring) == float:
print("Sorry, you typed an integer")
else:
print("The word you typed has",string_lenght(mystring), "characters")
I'm Newbie at Python. I really appreciate your help and patience.
Best regards.
input() always returns a string so you can try to convert it into int/float, if the operation is successful then it's a number, else it is a string:
try:
float(mystring)
print("Sorry you typed an integer")
except ValueError:
# Rest of the code ...
This question already has answers here:
Given n, take tsum of the digits of n. If that value has more than one digit, continue reducing a single-digit number is produced
(4 answers)
Closed 1 year ago.
I have problem and trying to get next:
new_string = "35" #and this result must be like new_int = 3+5.
How im available to do this? I know the type conversion, but not a clue how i should do this.
As you are new to the python, i suggest you doing it using
int(new_string[0]) # 3
int(new_string[1]) # 5
So now you have 2 integers, you can to whatever you want
This question already has an answer here:
Python - How to break while loop after empty value in a int turning input? [duplicate]
(1 answer)
Closed 2 years ago.
I have been trying to simply turn an input() function into an integer as the title of this question suggests. I am essentially trying to run a program that takes in as many inputs as the user inputs, but when an empty string is inputted, it breaks out of a loop and returns the average of all inputted numbers. Currently, my code looks like this:
count = 0
sum = 0.0
number = 1.0
while number != 0:
number = int(input(""))
sum = sum + number
count += 1
if number == 0:
continue
if number == "":
break
else:
print("Average is {}".format(sum / (count-1)))
The issue i face is the error:
ValueError: invalid literal for int() with base 10: ''
Does anyone have a simple solution for this? I feel like i'm overlooking something rather simple?
if number == "":
break
In the case where you want this to happen, number got its value as int(input()). That is, the attempt to convert to int happens first. Since an empty string cannot be converted to int, this test is not reached before the exception is thrown.
You should test for the exception anyway, using try:/except:. But if you want to compare a string to a string, you need to do it at the point where you still have the strings you want to do the comparison with.
This question already has answers here:
Convert Python strings into floats explicitly using the comma or the point as separators
(3 answers)
Convert decimal mark when reading numbers as input
(8 answers)
Closed 2 years ago.
Super beginner here.
I'm following along the Automate the Boring Stuff With Python book and I decided to make a little script to help me out with some basic percentage checking. I didn't want to open Excel everytime I wanted to do this.
So the script gets two inputs, an old price and a new price and then calculates the percentage change in price. I think I got that right.
The problem occurs once I try to enter a float (that's the right term, yeah?) and I use the comma here in Europe.
I found a topic here where a similar question was answered, but there seems to be an issue on whether or not to call setlocale and (as far as I understand it) it does not deal with how to convert an input?
My code is below:
def izracun_odstotkov(): #function to calculate the difference in % between original price and new price
while True:
try:
prvotna_cena = float(input('Prosim vnesi prvotno ceno:')) #original price
except ValueError:
print('Oprosti, to ni veljavni podatek. Vnesi stevilko.')
continue
if prvotna_cena == 0:
print('Prvotna cena ne more biti 0.')
else:
break
while True:
try:
nova_cena = float(input('Prosim vnesi novo ceno:')) #new price
except ValueError:
print('Oprosti, to ni veljavni podatek. Vnesi stevilko.')
continue
else:
break
print(round((float (nova_cena)- float (prvotna_cena))/ float (prvotna_cena)*100, 2), '%')
while True:
izracun_odstotkov() #This makes the script run over and over so the user can just keep checking the % changes.
You can use the replace method:
prvotna_cena = float(input('Prosim vnesi prvotno ceno:').replace(',','.'))