简体   繁体   English

我的代码不允许一个字符出现 2 次(如何修复)

[英]My code doesn't allow 2 times a character (how to fix)

I need to write a code that counts the amount of closed area's en the amount of ends within a word (so B has 2 closed area's) but when 1 character sits 2 times within 1 question it only counts 1 time.我需要编写一个代码来计算封闭区域的数量和一个单词内的结尾数量(所以 B 有 2 个封闭区域)但是当 1 个字符在 1 个问题中出现 2 次时,它只计算 1 次。

I tried something that should count the amount of characters but that just gived me more errorzs我尝试了一些应该计算字符数的东西,但这只是给了我更多的错误

G = 0
Chosen_word = str(input("Choose a word of max 60 character(only uppercase)"))
if "A" in Chosen_word:
  U = U + 2
  G = G + 1

if you type AA it should print 4 ends en 2 closed area's but it prints 2 ends en 1 closed area如果你输入 AA,它应该在 2 个封闭区域中打印 4 个端点,但它在 1 个封闭区域中打印 2 个端点

You're only going through this code once - for the first letter.您只需要通过此代码一次 - 对于第一个字母。 To go through each letter , you need to use a loop (a for loop that goes through every character would be best here):要遍历每个字母,您需要使用一个循环(遍历每个字符的for循环在这里最好):

for letter in chosen_word:
    if letter == 'A':
        U = U + 2
        G = G + 1
    elif letter == 'B':
        ...
G = 0
U=0
Chosen_word = str(input("Choose a word of max 60 character(only uppercase)"))

n = Chosen_word.count("A")
U = n * 2
G = n 

print (U)
print (G)

OUTPUT:输出:

Choose a word of max 60 character(only uppercase)SADDSAAAA
10
5

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我的代码不起作用,我该如何解决? - My Code just doesn't work, how do I fix it? 为什么我的代码不让我打印它运行了多少次? - Why doesn't my code let me print how many times it's run? 我已经在 pyCharm 上测试了我的代码并且它可以工作,但是在 Leetcode 上它给了我一个 KeyError 并且不起作用。 我如何解决它? - I have tested my code on pyCharm and it works, but on Leetcode it gives me a KeyError and doesn't work. How do I fix it? 我该如何解决我的邮件不会被解密的问题 - How can i fix that my message doesn't get decrypted 如何修复Python不会打印我的所有字符串 - How to fix Python doesn't print all my string Python:我的代码出错,我不知道如何修复它 - Python: Error in my code and i don't know how to fix it Output 字符串的每个字符依次通过递归和切片,但我的代码没有任何 output - Output each character of the string in turn by recursion and slicing, but my code doesn't have any output Button function 中的文本没有出现在我的 phyton Tkinter 中。 我正在使用 MacBook。 有人知道如何修复我的代码吗? - the text in the Button function doesn't appear in my phyton Tkinter. I'm using a MacBooks. anyone has any idea how to fix my code? 如何修复我的代码,以便在输入字符时只找到一个单词的位置而不是包含一个字符的单词? - How could I fix my code, so that only a word's position is found and not the word which contains a character when a character is inputted? 我的python代码运行了几次,但是一旦我关闭计算机或执行其他操作,它就不会再次运行 - My python code runs a few times but as soon as I close my computer or do something else, it doesn't run again
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM