Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
Why in gods name is it 57.5
compiler output
input return strings, thus 2 + 3 is 23 (the default behaviour for + with strings is to concatenate them), then the casting to int turns this into an actual 23, so finally you get 5 * 23 / 2 == 115 / 2 == 57.5.
To solve this, cast each parameter to int before doing the math operations:
int(h) * (int(a)+int(b)) / 2
You concatenate two string, then turn the result into an int. If you do int(a) + int(b) it will work properly.
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 months ago.
Improve this question
counttonum = 1
countnum = input("[•] Provide A Number :")
while counttonum < countnum:
print("[", counttonum, "] : Number = ", counttonum)
counttonum +=1
I was trying to make a counting tool that counts up to the provided number from the “input()” function.
For example:
providedNumberFromInput = 5
output = 1
2
3
4
5
And it’ll stop if the provided number is reached. Please help me.
You are very close to solution. Problem is that input() returns value as string so you will need to convert it. And also if you want to include entered number use <= instead of <
while counttonum <= int(countnum):
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
Let's say I have a bytearray of length 50 (50 bytes) but I want only the first 25 bytes from the bytearray. How do I do that?
For example:
c = bytearray(b'1703020030f19322e5cc9b9e56cb71d2ebcd888582913f7f13')
or
d= bytearray(b'\x17\x03\x03\x000\xd9O\x8a\x08L\t\x05:\xf6\xa0\x0b\xc0\xb6\xcc\xf5\x1a\xc5S\xf9\x98\xf4\\gTf\xcco\xc7\x10\x16\x1f\xf5\xcd`\x9f=K.\x8aj\x0b]\x9eW\xd0\x04\x17\xcd')
len(c) = 50 and len(d) = 53.
How do I just extract the first 50 bytes of it and discard the rest?
Thanks in advance!
A bytearray is a sequence, so you can just slice it:
d = d[:50]
Or, to avoid unnecessary copies if performance is critical and the bytearray will usually be shorter than your limit:
if len(d) > 50:
d = d[:50]
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
Write a shell (text-based) program, called sum_num.py, that asks the user for a semicolon (;) separated list of numbers, and calculated the total. See the example below.
a = str(raw_input("Enter semicolon separated list of integers:"))
b = a.split(";")
c = (a[0:])
print("the total is " + sum(c))
PS C:\Users\ssiva\Desktop> python sum_num.py
Enter semicolon separated list of integers: 3;10;4;23;211;3
The total is 254
This code will convert into integers and sum them
a=input()
b=a.split(';')
sum=0
for num in b:
sum+=int(num)
print(sum)
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I am finding difficulty performing a bitwise & operation between the bin() and int in binary form. How can I achieve this goal?
If you are trying to perform the bitwise AND operation (using the & operator) on two integers, there is no need to convert them to strings of their binary representations.
Take this example:
x = 4 # 0b100
y = 5 # 0b101
print(x & y) # => 4, which is 0b100, because 0b100 & 0b101 = 0b100
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
What is the logic behind
int("string", integer)
for example:
int("220", 3)
yields
24
The optional integer is the numeric base to use in converting the string (defaults to base 10).
220 base 3 = 2 * (3**2) + 2 * (3**1) + 0 * (3**0)
= 2*9 + 2*3 + 0*1
= 18 + 6 + 0
= 24
the int() method in python takes two inputs. First is the string you want to evaluate the second is the base.
In your example the base is three therefore:
1*0 = 0
3*2 = 6
9*2 = 18
0 + 6 + 18 = 24