+ 2
How to get an user confirmation in Python?
I want something similar as โconfirm()โ in javascript. And if you also know how to give the answer with fractions and not decimals, or to convert. https://code.sololearn.com/ck5etOhJxKVi/?ref=app
15 Answers
+ 1
oh sorry, I wanted to say โconfirm()โ
+ 1
If you want answers like 3/2 instead of 1.5, you can use the fractions module.
Write this at the top:
from fractions import Fraction
and then whichever variable you want to be written as a fraction, just put it inside Fraction(). For example, you can do str(Fraction(x1)) and str(Fraction(x2)) in your print statement.
Full documentation here:
https://docs.python.org/3/library/fractions.html?
Regarding confirm(), I'm not sure I understand you completely. But Sololearn's python only allows us to input everything at the same time. So I don't think you can display a dialog box with Okay/Cancel option for the user.
+ 1
Artur The Fraction() function will show 2.0 as 2 (and NOT 2/1), and 1.5 as 3/2. So it should be good for you!
+ 1
You have a spelling mistake in line 5. Change inpit to input.
When you input something, it is recognized as a string. You have to turn it into a float or int (depending on what you expect the input to be) before doing calculations. Same happens in Javascript. After getting an input, we do parseInt() before performing arithmetic.
So here we can do
a = float(input())
OR
a = int(input())
On Sololearn, I usually omit the message "write a" inside input(). It's not displayed anyway, so it doesn't matter.
0
I just want fractions if the number is a decimal, if is entire no
0
thank you so much, what a pity we canโt use confirm :(
0
and you know why when i use a= input(โwrite aโ) happens an error?
0
if write a number yes
0
Post your code so we can see what's wrong.
0
This code runs fine! Where's the error?
0
I didn't save, 1 second
0
I have corrected it but it only lets me write once and error...
0
Input three numbers in separate lines. Like this:
1
2
3