Python 3 - Temperature Converter - python-3.x

I am fairly new to any programing and I am trying to build a temperature converter function in python but I keep getting errors.
I wanted the function to first prompt the user for a temperature in Fahrenheit and convert it to Celsius;and next prompt the user for a temperature in Celsius and convert it to Fahrenheit. Below is my code. Any help will be appreciated
def main():
F = input("Input the temperature in Fahrenheit: ")
C = (5 / 9) * (float(F - 32))
print(int(C))
input("input the temperature in Celsius")
F = 9/5 * (float(C + 32))
Print(int(F))
main()
This is the error message I get:
Traceback (most recent call last):
File "/Users/mabook/Library/Preferences/PyCharmCE2018.2/scratches/scratch_7.py", line 11, in <module>
main()
File "/Users/mabook/Library/Preferences/PyCharmCE2018.2/scratches/scratch_7.py", line 4, in main
C = (5 / 9) * (float(F - 32))
TypeError: unsupported operand type(s) for -: 'str' and 'int'

The reason for your error as stated in the comments is that you can't take a string from an integer. So you need to convert you input to some type that you can take away from an integer like a float but you must do this before doing the calculation.
There are other things that need to be changed to make this code work:
print should be lower case
The second input needs to be stored in a variable so we can then use it
The brackets need to be changed when converting to Fahrenheit to make the formula correct
Unless you have some need to truncate the number back to an int and get rid of everything after the decimal point we may as well just print the result as is. If you do need the result as an int you should consider rounding to the nearest integer and not just truncating the value
With all these changes the code could look something like this:
def main():
F = input("Input the temperature in Fahrenheit: ")
C = (5 / 9) * (float(F) - 32)
print(C)
C = input("input the temperature in Celsius: ")
F = (9 / 5) * float(C) + 32
print(F)
main()

Related

Problem in the function of my program code python

I tried to make a program to do the below things but apparently, the function doesn't work. I want my function to take two or more arguments and give me the average and median and the maximum number of those arguments.
example input:
calc([2, 20])
example output : (11.0, 11.0, 20)
def calc():
total = 0
calc = sorted(calc)
for x in range(len(calc)):
total += int(calc[x])
average = total / len(calc)
sorted(calc)
maximum = calc[len(calc) - 1]
if len(calc) % 2 != 0:
median = calc[(len(calc) // 2) + 1]
else:
median = (float(calc[(len(calc) // 2) - 1]) + float(calc[(len(calc) // 2)])) / 2
return (average, median, maximum)
There are some things I'm going to fix as I go since I can't help myself.
First, you main problem is arguments.
If you hand a function arguments
calc([2, 20])
It needs to accept arguments.
def calc(some_argument):
This will fix your main problem but another thing is you shouldn't have identical names for your variables.
calc is your function name so it should not also be the name of your list within your function.
# changed the arg name to lst
def calc(lst):
lst = sorted(lst)
# I'm going to just set these as variables since
# you're doing the calculations more than once
# it adds a lot of noise to your lines
size = len(lst)
mid = size // 2
total = 0
# in python we can just iterate over a list directly
# without indexing into it
# and python will unpack the variable into x
for x in lst:
total += int(x)
average = total / size
# we can get the last element in a list like so
maximum = lst[-1]
if size % 2 != 0:
# this was a logical error
# the actual element you want is mid
# since indexes start at 0
median = lst[mid]
else:
# here there is no reason to explicity cast to float
# since python division does that automatically
median = (lst[mid - 1] + lst[mid]) / 2
return (average, median, maximum)
print(calc([11.0, 11.0, 20]))
Output:
(14.0, 11.0, 20)
Because you are passing arguments into a function that doesn't accept any, you are getting an error. You could fix this just by making the first line of your program:
def calc(calc):
But it would be better to accept inputs into your function as something like "mylist". To do so you would just have to change your function like so:
def calc(mylist):
calc=sorted(mylist)

Getting Integer division and modulo by zero error

I'm getting Integer Division or Modulo by zero error.
def getProduct(n):
product = 1
while (n != 0):
product = product * (n % 10)
n = n // 10
return product
def printSubstrings(n):
s=int(math.log10(n))
d=(math.pow(10,s))
k=d
count = 0
while n>0:
while d>0:
ans=0
ans = getProduct(n//d)
if ans%4==0 or ans%2!=0:
count+=1
d=int(d/10)
n = int(n%k)
k = int(k//10)
d = k
print(count)
Simple Inputs are running well but On entering Large input data it gives ZERODIVISIONERROR
on large input
10
11903030 2093524 04935049 09024 12242910 109310 1000901 103412 102901 10290191
Error I get:
Traceback (most recent call last):
File "e:/CodeWork/Code Challenge/rough.py", line 271, in <module>
printSubstrings(num)
File "e:/CodeWork/Code Challenge/rough.py", line 261, in printSubstrings
n = int(n%k)
ZeroDivisionError: integer division or modulo by zero
You need to make sure k != 0 holds before you compute n % k.
Since the modulus operator % is implemented by finding the remainder upon division, you will get a division by zero error if you try to evaluate n % 0 for any integer n.

Divide by Zero in Mean()?

I'm trying to write some code to compute mean, Variance, Standard Deviation, FWHM, and finally evaluate the Gaussian Integral. I've been running into a division by zero error that I can't get past and I would like to know the solution for this ?
Where it's throwing an error I've tried to throw an exception handler as follows
Average = (sum(yvalues)) / (len(yvalues)) try: return (sum(yvalues) / len(yvalues))
expect ZeroDivisionError:
return 0
xvalues = []
yvalues = []
def generate():
for i in range(0,300):
a = rand.uniform((float("-inf") , float("inf")))
b = rand.uniform((float("-inf") , float("inf")))
xvalues.append(i)
### Defining the variable 'y'
y = a * (b + i)
yvalues.append(y) + 1
def mean():
Average = (sum(yvalues))/(len(yvalues))
print("The average is", Average)
return Average
def varience():
# This calculates the SD and the varience
s = []
for i in yvalues:
z = i - mean()
z = (np.abs(i-z))**2
s.append(y)**2
t = mean()
v = numpy.sqrt(t)
print("Answer for Varience is:", v)
return v
Traceback (most recent call last):
File "Tuesday.py", line 42, in <module>
def make_gauss(sigma=varience(), mu=mean(), x = random.uniform((float("inf"))*-1, float("inf"))):
File "Tuesday.py", line 35, in varience
t = mean()
File "Tuesday.py", line 25, in mean
Average = (sum(yvalues))/(len(yvalues))
ZeroDivisionError: division by zero
There are a few things that are not quite right as people noted above.
import random
import numpy as np
def generate():
xvalues, yvalues = [], []
for i in range(0,300):
a = random.uniform(-1000, 1000)
b = random.uniform(-1000, 1000)
xvalues.append(i)
### Defining the variable 'y'
y = a * (b + i)
yvalues.append(y)
return xvalues, yvalues
def mean(yvalues):
return sum(yvalues)/len(yvalues)
def variance(yvalues):
# This calculates the SD and the varience
s = []
yvalues_mean = mean(yvalues)
for y in yvalues:
z = (y - yvalues_mean)**2
s.append(z)
t = mean(s)
return t
def variance2(yvalues):
yvalues_mean = mean(yvalues)
return sum( (y-yvalues_mean)**2 for y in yvalues) / len(yvalues)
# Generate the xvalues and yvalues
xvalues, yvalues = generate()
# Now do the calculation, based on the passed parameters
mean_yvalues = mean(yvalues)
variance_yvalues = variance(yvalues)
variance_yvalues2 = variance2(yvalues)
print('Mean {} variance {} {}'.format(mean_yvalues, variance_yvalues, variance_yvalues2))
# Using Numpy
np_mean = np.mean(yvalues)
np_var = np.var(yvalues)
print('Numpy: Mean {} variance {}'.format(np_mean, np_var))
The way variance was calculated isn't quite right, but given the comment of "SD and variance" you were probably going to calculate both.
The code above gives 2 (well, 3) ways to do what I understand you were trying to do but I changed a few of the methods to clean them up a bit. generate() returns two lists now. mean() returns the mean, etc. The function variance2() gives an alternative way to calculate the variance but using a list comprehension style.
The last couple of lines are an example using numpy which has all of it built in and, if available, is a great way to go.
The one part that wasn't clear was the random.uniform(float("-inf"), float("inf"))) which seems to be an error (?).
You are calling mean before you call generate.
This is obvious since yvalues.append(y) + 1 (in generate) would have caused another error (TypeError) since .append returns None and you can't add 1 to None.
Change yvalues.append(y) + 1 to yvalues.append(y + 1) and then make sure to call generate before you call mean.
Also notice that you have the same error in varience (which should be called variance, btw). s.append(y)**2 should be s.append(y ** 2).
Another error you have is that the stacktrace shows make_gauss(sigma=varience(), mu=mean(), x = random.uniform((float("inf"))*-1, float("inf"))).
I'm pretty sure you don't actually want to call varience and mean on this line, just reference them. So also change that line to make_gauss(sigma=varience, mu=mean, x = random.uniform((float("inf"))*-1, float("inf")))

Convert text to decimal python3

I need to convert words to numbers for RSA cipher, so i found code which can convert text to decimal, but when i run it in terminal by python 3 i get:
Traceback (most recent call last):
File "test.py", line 49, in <module>
numberOutput = int(bit_list_to_string(string_to_bits(inputString)),2) #1976620216402300889624482718775150
File "test.py", line 31, in string_to_bits
map(chr_to_bit, s)
File "test.py", line 30, in <listcomp>
return [b for group in
File "test.py", line 29, in chr_to_bit
return pad_bits(convert_to_bits(ord(c)), ASCII_BITS)
File "test.py", line 14, in pad_bits
assert len(bits) <= pad
AssertionError
when i use "python convert_text_to_decimal.py" in terminal it works correctly.
Code:
BITS = ('0', '1')
ASCII_BITS = 8
def bit_list_to_string(b):
"""converts list of {0, 1}* to string"""
return ''.join([BITS[e] for e in b])
def seq_to_bits(seq):
return [0 if b == '0' else 1 for b in seq]
def pad_bits(bits, pad):
"""pads seq with leading 0s up to length pad"""
assert len(bits) <= pad
return [0] * (pad - len(bits)) + bits
def convert_to_bits(n):
"""converts an integer `n` to bit array"""
result = []
if n == 0:
return [0]
while n > 0:
result = [(n % 2)] + result
n = n / 2
return result
def string_to_bits(s):
def chr_to_bit(c):
return pad_bits(convert_to_bits(ord(c)), ASCII_BITS)
return [b for group in
map(chr_to_bit, s)
for b in group]
def bits_to_char(b):
assert len(b) == ASCII_BITS
value = 0
for e in b:
value = (value * 2) + e
return chr(value)
def list_to_string(p):
return ''.join(p)
def bits_to_string(b):
return ''.join([bits_to_char(b[i:i + ASCII_BITS])
for i in range(0, len(b), ASCII_BITS)])
inputString = "attack at dawn"
numberOutput = int(bit_list_to_string(string_to_bits(inputString)),2) #1976620216402300889624482718775150
bitSeq = seq_to_bits(bin(numberOutput)[2:]) #[2:] is needed to get rid of 0b in front
paddedString = pad_bits(bitSeq,len(bitSeq) + (8 - (len(bitSeq) % 8))) #Need to pad because conversion from dec to bin throws away MSB's
outputString = bits_to_string(paddedString) #attack at dawn
So when i use just python he have 2.7 version. Please, help me fix this code to python 3
Change line 22,
n = n / 2
to
n = n // 2
This solves the immediate error you get (and another one that follows from it). The rest of the routine may or may not work for your purposes; I did not check any further.
You get the assert error because the function convert_to_bits should be, theoretically speaking, returning a proper list of single bit values for a valid integer in its range. It calculates this list by dividing the integer by 2 until 0 remains.
However.
One of the more significant changes from Python 2.7 to 3.x was the behavior of the division operator. Prior, this always returned an integer, but with Python 3 it was decided to have it return a float instead.
That means the simple bit calculation loop
while n > 0:
result = [(n % 2)] + result
n = n / 2
does not return a steady list of 0s and 1s anymore, always ending because the source integer runs out of numbers, but instead you get a list of more than a thousand floating point numbers. At a glance it may be unclear what that list represents, but as it ends with
… 1.03125, 0.0625, 0.125, 0.25, 0.5, 1]
you can see it's the divide-by-two loop that keeps on dividing until its input finally runs out of floating point accuracy and stops dividing further.
The resulting array is not only way, way larger than the next routines expect, its data is also of the wrong type. The values in this list are used as an index for the BITS tuple at the top of your code. With the floating point division, you get an error when trying to use the value as an index, even if it is a round 0.0 or 1.0. The integer division, again, fixes this.

How to make a binary string k-bits long

So what the function does is ask for input for the file name, k-bit binary strings, and n number of times to write k-bit binary strings.
def makeStrings():
fileName = str(input("File Name: "))
k = input("Bits Input: ")
n = int(input("Number of Strings: "))
outputFile = open(fileName, "w")
counter = 0
while (counter < n):
randomNumber = random.randint(0, 9223372036854775808) #that big number is the max representation of a 64bit binary string
binary = ('{:0' + str(k) + 'b}').format(randomNumber)
outputFile.write(str(binary) + "\n")
counter = counter + 1
outputFile.close()
So what my issue is is that the function works. It does what it is suppose to do except that it doesn't format the binary to be k-bits long. so say for instance I could have any binary representation from random numbers, but I only want them to be k-bits long. so if I make k = 8 it should give me random 8-bit binary strings that are 8 bits long or if I make k = 15 it should give me random 15-bit binary strings.
So say for instance my input is
>>> FileName: binarystext.txt #Name of the file
>>> 16 #number of bits for binary string
>>> 2 #number of times the k-bit binary string is written to the file
It should write to the file the following
1111110110101010
0001000101110100
the same representation would be applied for any bit binary string. 2, 3, 8, 32 etc.
I thought of maybe splitting all the numbers into their represented binary forms so like 0-255 for 8 bit for example and I would just format it if k = 8, but that would mean I would have to do that a ton of times.
Right now what I get is
1010001100000111111001100010000001000000011010111
or some other really long binary string.
The problem is that width in format string is the minimum width of the field:
width is a decimal integer defining the minimum field width. If not specified, then the field width will be determined by the content.
You could just limit the number you're randomizing to fix the issue:
randomNumber = random.randint(0, 2 ** k - 1)
Use random.getrandbits() and a format string:
>>> import random
>>> random.getrandbits(8)
41
>>>
>>> s = '{{:0{}b}}'
>>> k = 24
>>> s = s.format(k)
>>> s.format(random.getrandbits(k))
'101111111001101111100100'
>>>

Resources