gpt4 book ai didi

python - 限制字符串的字符

转载 作者:行者123 更新时间:2023-12-01 05:00:27 24 4
gpt4 key购买 nike

我在尝试编写一个程序时遇到一些问题,该程序接受输入(字符串)并检查字符串中的字符数。该字符串应该能够判断消息包含的字符数是超过还是少于 160 个。我已经写了代码,但它不会运行。有谁能够帮助我?我最好的猜测是使用 len() 函数,然后使用 if 或 else 语句。这是正确的做法吗?如何让 if 和 else 语句知道我从 len() 获得的数字是字符数?

我希望这有点道理......

正如你可能已经猜到的,我对 Python 还很陌生......希望大家能理解我的问题,还有的话欢迎提问!

我的代码:

def CharacterRestriction(): 
print("Welcome to the chat. Maximun amount of characters is: 160")
len(Message) = input("Enter your message:")
print ("The lenght of the message is:", len(Message))
if len(Message) <= 160:
print(Message)
else:
len(Message) > 160
print ("Maximum amount of characters are 160")
CharacterRestriction()

最佳答案

修正后的方法:

def CharacterRestriction(): 
print("Welcome to the chat. Maximun amount of characters is: 160")
Message = input("Enter your message:")
print ("The lenght of the message is:", len(Message))
if len(Message) <= 160:
print(Message)
else:
print("Maximum amount of characters are 160")

关于python - 限制字符串的字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26341297/

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