gpt4 book ai didi

Python:如何检查字符串中的内容是否正确

转载 作者:行者123 更新时间:2023-11-28 21:49:53 24 4
gpt4 key购买 nike

我是 python 的新手,如果我听起来很愚蠢,抱歉

因此,您可以检查其中的某些数字是否正确,而不是检查字符串中的每个数字吗?这是一个例子

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

所以如果有人可以做一个更长版本的 pi,而不是像这样做:

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

然后我可以做一些只知道 50 位数字的 pi,然后检查其中的一些是否正确吗?

最佳答案

您可以从 math 导入 pi,将其转换为字符串,并检查它是否以用户输入开头:

from math import pi

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

关于Python:如何检查字符串中的内容是否正确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32810698/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com