[英]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.