Search code examples
pythonstringvariablespi

Python: How do I check if something in a string is right


I am pretty new to python, sorry if I sound extremely stupid

So, instead of checking every single digit in a string, can you then check if some of it is right? Here is an example

pianswer = input ("What is pi?")
if pianswer == "3.14":
print ("That is correct")

So if somebody can do a longer version of pi, instead of doing something looking like this:

pianswer = input ("What is pi?")
if pianswer == "3.14":
    print ("That is correct")
elif pianswer == "3.141592":
    print ("That is correct")
elif pianswer == "3.1415":
    print ("That is correct

Can I then do something that just knows pi with like 50 digits and then checks if some of it is right?


Solution

  • You could import pi from math, convert it to a string, and check if it starts with the user's input:

    from math import pi
    
    pianswer = input ("What is pi?")
    if str(pi).startswith(pianswer):
        print ("That is correct")