Python function that gives a maximum of 1.2 [closed] - python

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I have this code here:
def maximum():
i = int(float(input("What is the maximum value?")))
if i < 1.2:
return print("ERROR: Maximum should be greater than 1.2"), maximum()
else:
return i
maximum()
But it doesn't let me use numbers between 0-1.9 showing an output of
What is the maximum value?1.2
ERROR: Maximum should be greater than 1.2
What should I change in the function to allow decimals to be accepted?

Remove the conversion to an int, as this will floor your float.
def maximum():
i = float(input("What is the maximum value?"))
if i < 1.2:
return print("ERROR: Maximum should be greater than 1.2"), maximum()
else:
return i
maximum()

int means integer, i.e. a whole number. When you call int, you're truncating (rounding down) to the nearest whole number, so any numbers from 1.0 to 1.9 just get truncated to 1, which is less than 1.2. You don't need the int call if you want a fractional number.
i = float(input("What is the maximum value?"))

Related

Why my output result doesnt show 0 when it's supposed to show exact number in reverse order in Python.Without changing parameter variable to string [closed]

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 1 year ago.
Improve this question
Why my output result doesn't show 0 when it's supposed to show exact number in reverse order in Python.Without changing the parameter variable to string.
parameter = int(input())
sum = 0
while parameter > 0:
digit = parameter % 10
sum = sum * 10 + digit
parameter = parameter // 10
print(sum)
A couple of things are going on here. First, you're converting the input to an int. This means that when reversing the number you'll lose all trailing 0s. This is because for an int there is no reason to show a leading 0. Instead, you can simply print out each digit in reverse. Another thing to note is you shouldn't use sum as a variable since it will overwrite the built in sum function.
parameter = int(input())
while parameter > 0:
digit = parameter % 10
print(digit, end='')
parameter = parameter // 10
print("")
You can do this by printing the output right-justified in a field of the same width as the original number. This doesn't really avoid the fact that string manipulation is required, but at least it allows you to store the number as a number and not a string:
from math import ceil, log10
...
digits = ceil(log10(parameter + 1)) or 1
...
print(f'{sum:>0{digits}}')

Check if the length of the range divided by range element has no division remainder [closed]

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
The code below shall show a specific comment if the number of elements in the array isn't integer-type number.
It works for dr=0.5 but not for dr=0.1 because print (4%0.1) returns 0.09999999999999978 instead of 0.
How can I change the code to get 4%0.1==0?
import math
limits_real=(-2,2)
dr=0.1
if (limits_real[1]-limits_real[0])%dr!=0:
print ('Inapropriate limits or elements')
It works for dr=0.5 but not for dr=0.1 because print (4%0.1) returns 0.09999999999999978 instead of 0.
Because floating point numbers have limitations that every programmer should know, see this:
https://docs.python.org/3/tutorial/floatingpoint.html
As showed at the end of above documentation, you can use decimal module, which works exacly right but is slower than normal floating point arihtmetics:
from decimal import Decimal
limits_real=(-2,2)
dr = Decimal("0.1")
if (limits_real[1] - limits_real[0]) % dr != 0:
print ('Inapropriate limits or elements')
Note that you should use a str while constructing the Decimal instance, do not use a float.
I found out only this solution :) At least it works
import math
limits_real=(-2,2)
dr=0.1
if (((limits_real[1] - limits_real[0]) * 1000) % (dr * 1000)) / 1000 != 0:
print ('Inapropriate limits or elements')

How to print inputs in new line [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
Hey is there a way to print inputs like this:
Input:
Enter the minimum number of feet (not less than 0):
5
Enter the maximum number of feet (not more than 99):
10
How can one do this?
Simply add a new line to the input prompt with \n (Python newline character) like this:
minFeet = input("Enter the minimum number of feet (not less than 0): \n")
maxFeet = input("Enter the maximum number of feet (not more than 99): \n")
You can just do it like this:
print("Enter the minimum number of feet (not less than 0):")
minN = input()
print("Enter the maximum number of feet (not more than 99):")
maxN = input()

How to convert a string of a decimal number from any base to a decimal number? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
I'm looking to write a function that can convert a string with a given base to a decimal number.
Let's say the function is convert calling convert should give me the following output
convert('3.14', base=10) ~= 3.14
convert('100.101', base=2) == 4.625
To convert floating-point numbers from one base to another, you can just break the number in half, handle the whole and the part separately, and then join them back together.
num = '100.101'
base = 2
# split into whole and part
whole = num[:num.index('.')]
part = num[num.index('.') + 1:]
# get the logarithmic size of the part so we can treat it as a fraction
# e.g. '101/1000'
denom = base ** len(part)
# use python's built-in base conversion to convert the whole numbers
# thanks #EthanBrews for mentioning this
b10_whole = int(whole, base=base)
b10_part = int(part, base=base)
# recombine the integers into a float to return
b10_num = b10_whole + (b10_part / denom)
return b10_num
Thanks to the other answerer #EthanBrews for mentioning that integer stuff was already built-in. Unfortunately the same construction doesn't same to exist for float.
Python supports already. Simply use my_int = int(str, base)

how to restrict user to input only upto two decimal point float numbers in python? [closed]

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
I am new in python, i want to do is limit the float input by the user only upto two decimal point, eg: 1.11, user is not allowed to input 1.111 or more than to after two decimal point . Thank you
You cannot restrict what the user inputs, but you can convert a float to have 2 decimal points:
value = float(input("Input your number: "))
print ("You inputted " + str(value))
new_value = "{:.2f}".format(value)
print ("After formatting, your number has become: " + str(new_value))

Categories

Resources