Here's the code i did:
list = input("Enter the number: ")
p = print("Trailing zeroes = ")
print(p, list.count("0"))
Output:
Enter the number: 10000
Trailing zeroes =
None 4
The output i wanted:
Enter the number: 10000
Trailing zeroes = 4
To count trailing zeroes you could do this:
num = input('Enter number: ')
ntz = len(s)-len(s.rstrip('0'))
print(f'Your input has {ntz} trailing zeroes')
Taking into account #OldBill 's comment & answer:
I corrected the code so that it gives an answer taking into account both of Op's problem.
The counting of the trailing zero is #OldBill 's code and not mine.
With
list = input("Enter the number: ")
p = print("Trailing zeroes = ")
print(p, list.count("0"))
What you do is p = print("Trailing zeroes = ") which is not a value. That explains the Nonewhen you print p.
Plus your counting of trailing zero doesn't work, and count all zeroes.
To have your code working, either you should have
num = input('Enter number: ')
ntz = len(num)-len(num.rstrip('0'))
print(f'Your input has {ntz} trailing zeroes')
or
num = input('Enter number: ')
print('Your input has', len(num)-len(num.rstrip('0')),' trailing zeroes')
Here is a working example of the code:
i = input("Enter the number: ")
p = "Trailing zeroes = " + str(i.count("0"))
print(p)
Output:
Enter the number: 1000
Trailing zeroes = 3
Another way to do this:
NB1: the input is a string not a list.
NB2: This will count all zeroes - not just trailing – (By #OldBill)
text= input("Enter the number: ")
print("Trailing zeroes = ", text.count("0"))
Related
def main():
Need the code to ask the user a number and print out number in words digit by digit.
Ex. Input: 473
Output: four seven three
numbers = ["zero","one","two","three","four","five","six","seven","eight","nine"]
n = eval(input('Please input a number: '))
print('You typed a number: ',n)
print('The number in words: ', numbers[n])
Cannot get code here to print out more than one digit. I have tried a few for loops and always get the error message with my index
main()
Because your list has 10 items and it can not find index for example 324
so you should split your number into digits and try it for each digit.
numbers = ["zero","one","two","three","four","five","six","seven","eight","nine"]
n = int(input('Please input a number: '))
print('You typed a number: ',n)
print('The number in words: ', end="")
for char in str(n):
print(numbers[int(char)], end=" ")
print()
You are basically trying to access an eleent in an array(your array only have 10 elements, so works fine for n<=10) that doesn't exist. You need to extract the digits, and try for every digit.
you can put numbers in a dictionary
numbers = {'0': 'zero', '1': 'one', '2': 'two', etc...}
then loop throw the user's string:
for number in n:
print(numbers[number] + " ", end='')
here is version that uses a list comprehension:
numbers = ["zero","one","two","three","four","five","six","seven","eight","nine"]
inp = input()
print(" ".join([numbers[int(dig)] for dig in inp]))
I need some help creating a python program that does the following:
Execution:
Enter an integer: 10
Enter an integer: 5
Enter an integer: 3
Enter an integer: 0
Numbers entered: 4
Sum: 18
Max: 10
Min: 0
I currently have this and I am stuck:
user_input = " "
while user_input != "":
user_input = input("Enter a number, or nothing to quit: ")
if user_input != "":
user_input = int(user_input)
user_input += 1
print(user_input)
Help would be much appreciated!
You can first collect the inputs in a list, and then print the results, where f-strings are handy:
lst = []
while (user_input := input("Enter a number (blank to quit): ").strip()):
lst.append(int(user_input))
print(f"You have entered {len(lst)} numbers:", *lst)
print(f"Sum: {sum(lst)}")
print(f"Max: {max(lst)}")
print(f"Min: {min(lst)}")
If you are not familiar with walrus operator :=, then
lst = []
user_input = input("Enter a number: ").strip() # ask for input
while user_input != '': # check whether it is empty (!= '' can be omitted)
lst.append(int(user_input))
user_input = input("Enter a number (blank to quit): ").strip() # ask for input
Example:
Enter a number (blank to quit): 1
Enter a number (blank to quit): 2
Enter a number (blank to quit): 3
Enter a number (blank to quit):
You have entered 3 numbers: 1 2 3
Sum: 6
Max: 3
Min: 1
Detailed version:
user_inputs_values = []
user_inputs_count = 0 # (a)
user_input = input("Enter a number: ")
user_inputs_values.append(int(user_input))
user_inputs_count += 1 # (a)
while True:
user_input = input("Enter another number, or enter to quit: ")
if not user_input:
break
user_inputs_values.append(int(user_input))
user_inputs_count += 1 # (a)
print(f"Numbers entered: {user_inputs_count}") # (b)
print(f"Sum: {sum(user_inputs_values)}")
print(f"Max: {max(user_inputs_values)}")
print(f"Min: {min(user_inputs_values)}")
Note that the first input message is different than the following ones.
Note using a count of entries is also possible, taas the count is equal to len(user_inputs_values). So you can remove lines marked (a) and replace line marked (b) with:
print(f"Numbers entered: {len(user_inputs_values)}")
I need to write a program that asks the user to enter a number, n, which is -6
The numbers needed to be printed using a field width of 2 and need to be right justified. Fields must be seperated by a single space, with no space after the final field.
so far this is what i have got to this:
n = int(input("Enter the start number: "))
if n>-6 and n<93:
for i in range(n, n+7):
print("{:>2}".format(i), end=" ")
But I still seem to be getting spacing issues, any help would be much appreciated!
n = int(input("Enter the start number: "))
if n>-6 and n<93:
for i in range(n, n+7):
if i < n + 6:
print("{:>2}".format(i), end=" ")
else:
print("{:>2}".format(i))
Solution
n = int(input("Enter the start number: "))
width = int(input('Enter width : '))
if n>-6 and n<93:
for i,val in enumerate(range(n,n+width)):
if i==(width-1):
print("{}".format(val))
else:
print("{}".format(val),
end=" ")
Output
Enter the start number: 5
Enter width : 5
5 6 7 8 9
def main():
num_list = []
n = input('Your favorite number: ')
again = 'g'
while again == 'g':
value = float(input('Enter a number: '))
if value > n:
num_list.append(value)
print ('Would you like to enter another number?: ')
again = input('y=yes, n=no')
main()
Here's my code. The > isn't working, what do i do?
I see two problems with your code,
As comments says, your comparing a float type to a str. Note that it is possible in Python 2 to compare mixed types for nonsensical answers, not in Python 3.
You're comparing your again variable against 'g' only. which is clearly not what you want. Try this code :
def main():
num_list = []
n = input('Your favorite number: ')
again = 'g'
while again in {'g','y'} :
value = float(input('Enter a number: '))
if value > float(n):
num_list.append(value)
print ('Would you like to enter another number?: ')
again = input('y=yes, n=no')
main()
I have this code for a program that should manipulate certain inputs the user enters.
I'm not sure how to only get x number of ouputs (x is specified by the user at the start of the program).
numOfFloats = int(input("Enter the number of floating point inputs: "))
numOfInts = int(input("Enter the number of integer inputs: "))
numOfStrings = int(input("Enter the number of string inputs: "))
for num in range(numOfStrings,0,-1):
print()
ffloats = float(input("Enter a real number: "))
iints = int(input("Enter an integer: "))
string = input("Enter a string: ")
print()
print("float: ", ffloats**(1/10))
print("int: ", iints**10)
print("string: ", (string + string))
I get all three requests each time, even though I have specified in the beginning that I only want 1 float, 2 ints, and 3 strings. I get asked for 3 floats, 3 ints, and 3 strings. I do realize what my code does, but I'm not sure how to get it to where I want it. I have a feeling something is wrong in the for loop conditions.
Any help is appreciated!
ffloats = []
for num in range(numOfFloats):
ffloats.append(float(input("\nEnter a real number: "))
iints = []
for num in range(numOfFloats):
iints.append(int(input("\nEnter an integer: "))
sstrings = []
for num in range(numOfFloats):
sstrings.append(input("\nEnter a real number: ")
print("Floats:", [f**(1/10) for f in ffloats])
print("Ints:", [i**10 for i in iints])
print("Strings:", [s + s for s in sstrings])
If you want them in order, then you'll have to:
for v in range(max([numOfFloats, numOfInts, numOfStrings])):
if v < numOfFloats:
ffloats.append(float(input("\nEnter a real number: "))
if v < numOfInts:
iints.append(int(input("\nEnter an integer: "))
if v < numOfStrings:
sstrings.append(input("\nEnter a string: ")
The program did exactly what you told it to do: given the number of strings -- 3 -- get that many int-float-string sets. You never used the other two quantities to control their loops. You need three separate loops; here's the one for strings, with all the int and float stuff removed.
numOfStrings = int(input("Enter the number of string inputs: "))
for num in range(numOfStrings,0,-1):
print()
string = input("Enter a string: ")
print()
print("string: ", (string + string))
Now just do likewise for ints and floats, and I think you'll have what you want.
Yes, you can do it in one loop, but it's inelegant. You have to find the max of all three numbers and use that as the loop's upper limit. Within the loop, check "num" against the int, float, and string limits, each in turn.
This code would be less readable, harder to maintain, and slower. Do you have some personal vendetta against loops? :-)
If your really want just a single loop, then I would suggest you use a while loop rather than a for loop, as you need to keep looping until all values have been entered.
numOfFloats = int(input("Enter the number of floating point inputs: "))
numOfInts = int(input("Enter the number of integer inputs: "))
numOfStrings = int(input("Enter the number of string inputs: "))
while numOfFloats + numOfInts + numOfStrings:
print()
if numOfFloats:
ffloats = float(input("Enter a real number: "))
if numOfInts:
iints = int(input("Enter an integer: "))
if numOfStrings:
string = input("Enter a string: ")
print()
if numOfFloats:
print("float: ", ffloats**(1/10))
numOfFloats -= 1
if numOfInts:
print("int: ", iints**10)
numOfInts -= 1
if numOfStrings:
print("string: ", (string + string))
numOfStrings -= 1
So for example:
Enter the number of floating point inputs: 1
Enter the number of integer inputs: 2
Enter the number of string inputs: 3
Enter a real number: 1.5
Enter an integer: 2
Enter a string: three
float: 1.0413797439924106
int: 1024
string: threethree
Enter an integer: 2
Enter a string: hello
int: 1024
string: hellohello
Enter a string: world
string: worldworld