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
Related
can anyone show me how I can accept integer and float for my input (in the list)? Because if I enter numbers in full, the result always comes out with a .0
Here is my code:
user = int(input("How many numbers do you want to sum up: "))
list = [float(input("Number: ")) for i in range (user)]
print("The Sum of the entered Numbers is: ", sum(list))
my output would look like this:
How many numbers do you want to sum up: 3
Number: 2
Number: 1
Number: 5
The Sum of the entered Numbers is: 8.0
You can do:
user = int(input("How many numbers do you want to sum up: "))
list = [float(input("Number: ")) for i in range (user)]
x = sum(list)
if x.is_integer():
x = int(x)
print("The Sum of the entered Numbers is: ", x)
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
I am trying to get my code to show each digit individually on its own line, and it does do that. However, I am getting an error at the end of it, so I want to detect exactly how many digits are in the number. I am having trouble finding a solution for this that isn't len(), because for this specific program I am not supposed to use it.
Here's my code:
number = int(input("Enter a positive integer: "))
number = str(number)
digits = 0
while True:
print(number[digits])
digits += 1
You can create a counter and with a while loop should look like:
num = int(input("enter num"
result = 0
while num > 10:
num = num // 10
result += 1
result += 1
print(result)
It throws an error because you are running an infinite while loop. Use for instead.
number = int(input("Enter a positive integer: "))
number = str(number)
digits = 0
for i in number:
print(i)
digits+=1
You can also use the log10 (logarithm with base 10) function:
import math
number = int(input("Enter a positive integer: "))
print(1 + int(math.log10(number)))
You can solve your problem just by printing elements of string concatenated with new-line delimiter without attempts to find how many elements in string:
number = input("Enter a positive integer: ")
print('\n'.join(number))
The reason you’re getting the error is because when the digits acquires the value of the length of the number say l, then number[l] doesn't exist (The indices start from 0)
number = input("Enter a positive integer: "))
# type(number) is <class 'str'> you don't have to convert to int and reconvert to string
# since <class 'str'> implements iterable you could go like this ..
digits = 0
for digit in number:
print(digit) # digit is still a str here
digits += 1
got a error while compiling the code.
I tried to find smallest and largest value from user's input by storing the input in lists. After 'int' object not iterate problem, couldn't proceed further
largest=0
smallest=0
num=[]
while True:
num = int(input("Please enter a number: "))
for i in num:
if i>largest:
largest=i
for j in num:
if j<smallest:
smallest=j
if num==12:
break
print(largest)
print(smallest)
The moment you issue below code line num is no longer a list, instead its an int type of data.
num = int(input("Please enter a number: "))
As you can understand, there is nothing to iterate over in case of a single integer value.
The right solution is to read your input to a separate variable and append to your list.
input_num = int(input("Please enter a number: "))
num.append(input_num)
Further you will have to change value of your exit clause
if num==12:
break
If you desire to stop the loop after 12 inputs, then use len(num) == 12 in the if condition. In case you want to break loop if input number is 12 then change if condition to if input_num == 12
Note: Your algorithm has logical errors as well. You are assigning smallest to 0 . In case user enters all positive integers as input, your result will incorrect.
You are trying to iterate through a number which is wrong, you have overwritten your num list to an integer. Instead of the following:
num = int(input("Please enter a number: "))
You should save the number in some other variable and add to num list like:
x = int(input("Please enter a number: "))
num.append(x)
I want to test whether a 10 digit number is of 10 length, but whenever the number begins with 0, len() only counts 9.
How can I fix this?
At the moment:
something is a variable made up of numbers, I converted the variable into a string, then made this statement.
if len(something) != 10:
(do something)
My current code:
number = int(input("Enter number: "))
number = str(number)
if len(number) != 10:
print ("Not 10 digits long")
else:
print ("Good")
If I inputted a number with 10 digits it's fine, BUT, when I input a number with 10 digits and starting with zero, the len() function recognizes the number as 9 long. Help plz
Providing yout code, it's because you are casting your input to int, then casting it down to string (input automatic type is str in Python3, if you're using Python2, don't forget to cast as str or using raw_input like hackaholic).
Replace
number = int(input("Enter number: "))
number = str(number)
By
number = input("Enter number: ")
So number will directly be a string. And you can use len() on it. It even works with 0000000000
you forcing it to integer, input take values as string
number = input("Enter number: ")
if len(number) != 10:
print ("Not 10 digits long")
else:
print ("Good")
len function works on string
if you using python 2.x better to use raw_input("Enter Number: ")
Numbers starting with 0 are represented in base 8 (octal numbers) in python. You need to convert them to string to count their digits :
# Not converted
number1 = 0373451234
print "number: "+str(number1)+" digits:"+str(len(str(number1)))
> number: 65950364 digits:8
# Converted
number2 = "0373451234"
print "number: "+str(number2)+" digits:"+str(len(str(number2)))
> number: 0373451234 digits:10