我创建这个算法是为了给用户提供替代方案来分析一系列不同的方法,但我对菜单有疑问

问题描述 投票:0回答:0

我无法运行菜单,每次我尝试运行时都会出现以下消息: TypeError Traceback(最后一次调用) 在() 73 74#Ler a opção escolhida pelo usuário ---> 75 opcao = 输入(海峡(“ Escolha uma opção: ")) 76 77 # Opção (a) Ler uma 字符串 S1

TypeError: 'str' 对象不可调用

我想无误地运行这个算法:

`> def ler_string():

s1 = input("数字字符串 S1 (tamanho máximo 20 caracteres): ") 当 len(s1) > 20 时: s1 = input("A string S1 deve ter no máximo 20 caracteres. Digite novamente: ") 返回 s1 def imprimir_tamanho(s1): print(f"O tamanho da string S1 é {len(s1)}")

def comparar_strings(s1): s2 = input("数字一个nova字符串S2:") 如果 s1 == s2: print("As strings são iguais") 别的: print("As strings são diferentes")

def concatenar_strings(s1): s2 = input("数字一个nova字符串S2:") s3 = s1 + s2 print(f"A concatenação de S1 e S2 é: {s3}")

def imprimir_reverso(s1): print(f"A string S1 em ordem reversa é: {s1[::-1]}")

def contar_caractere(s1): c = input("Digite o charactere a ser contado: ") print(f"O caractere '{c}' aparece {s1.count(c)} vezes na string S1")

def substituir_caractere(s1): c1 = input("数字字符替换为:") c2 = input("替换字符的数字:") s2 = s1.replace(c1, c2, 1) print(f"A nova string após a substituição é: {s2}")

def verificar_substring(s1): s2 = input("数字字符串 S2 a ser verificada: ") 如果 s2 在 s1 中: print("S2 é substring de S1") 别的: print("S2 não é substring de S1")

def retornar_substring(s1): inicio = int(input("在初始位置对子字符串进行数字化:")) tamanho = int(input("Digite o tamanho da substring: ")) s2 = s1[inicio:inicio+tamanho] print(f"A substring de S1 é: {s2}")

s1 = ''

虽然正确: 打印(” 菜单”) print("(a) Ler uma 字符串 S1") print("(b) Imprimir o tamanho da string S1") print("(c) Comparar a string S1 com uma nova string S2") print("(d) 连接字符串 S1 com uma nova 字符串 S2") print("(e) Imprimir a string S1 de forma reversa") print("(f) Contar quantas vezes um dado caractere aparece na string S1") print("(g) Substituir a primeira ocerrentia do caractere C1 da string S1 pelo caractere C2") print("(h) Verificar se uma string S2 e´ substring de S1") print("(i) Retornar uma substring da string S1") print("(q) Sair do programa")

opcao = 输入(“ Escolha uma opção: ")

如果 opcao == 'a': s1 = input("数字uma字符串:")

elif opcao == 'b': print("O tamanho da string S1 é:", len(s1))

elif opcao == 'c': s2 = input("数字 uma nova 字符串 S2:") 如果 s1 == s2: print("作为字符串 S1 e S2 são iguais") 别的: print("As strings S1 e S2 são diferentes")

elif opcao == 'd': s2 = input("数字 uma nova 字符串 S2: ") s1 = s1 + s2 print("A string resultante da concatenação é:", s1)

elif opcao == 'e': print("A string S1 de forma reversa é:", s1[::-1])

elif opcao == 'f': caractere = input("数字 um caractere:") ocorrencias = s1.count(caractere) print("O caractere", caractere, "aparece", ocorrencias, "vezes na string S1")

elif opcao == 'g': c1 = input("数字字符替换为:") c2 = input("替换字符的数字:") s1 = s1.replace(c1, c2, 1) print("Nova 字符串 S1:", s1)

elif opcao == 'h': s2 = input("数字 uma nova 字符串 S2:") 如果 s2 在 s1 中: print("S2 é uma substring de S1") 别的: print("S2 não é uma substring de S1")

elif opcao == 'i': inicio = int(input("初始数字:")) fim = int(input("最后的数字:")) 子串 = s1[初始值:fim] print("Substring de S1:", substring)

elif opcao == 'q': print("程序编码。") 打破`

python algorithm menu
© www.soinside.com 2019 - 2024. All rights reserved.