multiple float input() with for loop value - python-3.x

I am trying get floating point number from user input with for loop in python 3.6.7:
for _ in range(int(input())):
foo = float(input())
Input:
1
12.3
No error, But when it is more than one value Error shows up:
for _ in range(int(input())):
foo = float(input())
Input:
2
2.5 3.1
ValueError: Could not convert string to float: '2.5 3.1'
any thoughts? thanks in advance.

When you input something and press Enter, input treats that data as a single string. So, 3.141<hit Enter> is a single string "3.141", and it can be converted to a floating-point number with float.
However, 3.141 5926<hit Enter here> is a single string "3.141 5926". Is that a representation of a single (floating-point) number? It's not (there are two numbers), so float can't convert that to a single number because of the space.
If you want to treat these numbers separated by a space as individual numbers, split the string and then convert each number:
data = input().split() # gives ['3.141', '5926']
for x in data:
print(float(x)) # converts each string to a number

Related

convert a number or string to binary number in python?

sample input:
B15
sample output:
B15 in binary = 1011000010101
I've tried
a = input()
print(bin(a))
The hexadecimal number 0xB15 = 2837 has the binary representation 0b101100010101. So if your input is a hexadecimal number, you need to tell Python to convert the string "B15" which comes out of input() into the hexadecimal number 0xB15, also known as the decimal number 2837, before you can convert it into binary for output.
BASE = 16
a = int(input(), BASE)
print(bin(a)[2:]) # Cut of the first two characters '0b'

Getting Typerror: not all arguments converted during string formatting

Given question - Given a list of 10 numbers, find the average of all such numbers which is a multiple of 3
num = []
newlist = []
for i in range(1, 11):
num.append(input())
for i in num:
if i%3==0:
newlist.append(i)
length = len(newlist)
total = sum(newlist)
average = total/length
print(average)
Getting this type error below at line 9 i.e. if i%3==0
not all arguments converted during string formatting
input() returns a string, so i%3 will actually perform printf-style string formatting. Since your input doesn't have any formatting specifiers, but the % operator's right operand is not empty, you get the error, because you attempted to format a sting that doesn't have enough formatting specifiers.
To solve this, convert your input to integers:
num.append(int(input()))
When you num.append(input()), the value of input() is a string. You need to first convert that to an int and handle any possible errors before continuing. One way to do this is to change it to:
num.append(int(input()))
Since all the values in num are strings, i % 3 tries to perform old-string formatting, which is not what you expect.

Extracting Certain Numbers From an Int Value

i am trying to get the numbers in individual form added to a list.
For example, if i input 3245, i want the list to say
["3","2","4","5"]
I have tried using a for in statement but got back numbers ranging from 0 - 3244 which was expected.
Any insight would be greatly appreciated, i am very new to python and am trying to teach myself code and re-write all my projects for school that was done in c to turn them into python. NOTE: i am using python 3, not 2.
Here is the rest of my code if it helps.
cc = []
card = int(input("Credit Card: "))
for n in range(card):
cc.append(n)
print(cc)
First of all, you should either accept the input number as string or convert it to string. That way, you can just parse through each character in the string and add them to the list. Currently you are getting the number 0-3244 because of you are looping for the amount of inputted number and adding the loop index to your list. Therefore, this should do what you want
cc = []
card = input("Credit Card: ") # or str(int(input("Credit Card: ")))
# if you want to restrict it to integer
for n in range(len(card)): # loop by number of characters
cc.append(card[n]) # append each character
print(cc)
a = 3245
b = list(str(a))
print(b)
The above code can convert an integer to a list of characters. First convert the integer to a string and then convert the string to a list form.
you can just convert the integer to string and iterate through every character of the string and during the iteration just append to cc.
cc = []
card = int(input("Credit Card: "))
for i in str(card):
cc.append(i)
print(cc)

Converting int to string then back to int

How do I call out a particular digit from a number. For example: bringing out 6 from 768, then using 6 to multiply 3. I've tried using the code below, but it does not work.
digits = []
digits = str(input("no:"))
print (int(digits[1] * 5))
If my input is 234 since the value in[1] is 3, how can I multiply the 3 by 5?
input() returns a string (wether or not you explicitly convert it to str() again), so digits[1] is still a single character string.
You need to convert that single digit to an integer with int(), not the result of the multiplication:
print (int(digits[1]) * 5)
All I did was move a ) parenthesis there.
Your mistake was to multiply the single-character string; multiplying a string by n produces that string repeated n times.
digits[1] = '3' so digits[1] * 5 = '33333'. You want int(digits[1]) * 5.

MATLAB: Concatenate number value as string

I want to create a binary number in matlab and am having difficulty concatenating the numbers.
Here's what I tried so far:
testarray = zeros(10,10)
testarray = num2str(testarray) % Convert all values to type string
testarray(1,1) = num2str(1); % Fill with abitrary value
testarray(1,1) = strcat(testarray(1,1), num2str(0)); % Trying to make '10' here but instead I get this error: "Assignment has more non-singleton rhs dimensions than non-singleton subscripts"
Any help would be appreciated.
In your example, the problem is that '10' has size [1,2], but testarray(1,1) has size [1,1]. So you might consider using cells instead:
testarray = cell(5,5);
testarray{1,1} = strcat(testarray(1,1), num2str(0));
By the way, you should have a look at the function dec2bin.
From the documentation:
dec2bin(23)
ans =
10111
The resulting value is a string.
So if you want to concatenate two binary values (encoded as strings), just do:
['10' '11']
ans =
1011

Resources