mardi 12 juillet 2016

User inputs how many decimals to generate [duplicate]

This question already has an answer here: Can a variable be used in Python to define decimal places 2 answers I wanted to make a program in python in which you could have the user decide how many decimal places to round to. For example, if they chose pi, they could also choose how many decimal places of pi they wanted to see. import math decimalPlaces = input("Enter the number of decimal places you wish to see for pi: ") int(decimalPlaces) But then how do I go about using the decimalPlaces to print pi? The best I could think of is: print("Pi is %.%if" % (decimalPlaces, math.pi)) But obviously that doesn't work because the %i is getting in the way of the %f I apologize if this question is poorly worded, I'm not exactly sure how to convey my question.

Aucun commentaire:

Enregistrer un commentaire