0
s = input()
i = 0

while i < len(s) and (s[i]) < "A" or "Z" < s([i]):
    print(i)
    

I keep getting this wrong and I'm not sure what to do. I Do not want to use a for loop just a while loop. Thank you

RTStriker
  • 5
  • 3
  • 1
    `and (s[i]) < "A" or "Z"` is not at all doing what you think it is. Does this answer your question? [How to test multiple variables against a value?](https://stackoverflow.com/questions/15112125/how-to-test-multiple-variables-against-a-value) – Random Davis Dec 09 '20 at 16:12
  • 1
    `sum(c.isupper() for c in s)` – Samwise Dec 09 '20 at 16:13
  • python has a string [isupper()](https://docs.python.org/3/library/stdtypes.html#str.isupper) function that avoids having to do `<` and `>` operations – G. Anderson Dec 09 '20 at 16:14
  • do you mean (s[i]) >= "A" and "Z" >= s([i]) – Z Li Dec 09 '20 at 16:14

5 Answers5

2

You can do it by many ways.

If I were you I will do it using isupper() and sum() generator,

s = input("Type something...")
print(sum(1 for c in s if c.isupper()))

Using while as you asked,

s = input("Type something...")
i = 0
capital = 0
while i < len(s):
    if s[i].isupper():
        capital+=1
    i = i + 1
print(capital)
A l w a y s S u n n y
  • 36,497
  • 8
  • 60
  • 103
  • 1
    The question states use while loop – JacksonPro Dec 09 '20 at 16:16
  • @JacksonPro how come? You never encounter bad posts? Always Sunny that's my down-vote as the question specifically asks to do it with a while loop... Simply giving other ways doesn't help OP. They have a specific problem they need help with. Anyway, alternate ways exists in duplicates, so it's better you close the question instead of answering (such as https://stackoverflow.com/a/18129868/6045800) – Tomerikoo Dec 09 '20 at 16:20
  • 1
    @Tomerikoo I didn't upvote this answer either. I have indeed come across bad posts, downvoting posts will also cost you your points. So instead of downvoting bad posts, I upvote the good ones. Read it here: https://meta.stackexchange.com/questions/15863/why-do-you-lose-reputation-for-downvoting-answers – JacksonPro Dec 09 '20 at 16:40
  • @JacksonPro it shouldn't be instead... Both actions are not related. It's nice that you up-vote good posts but should also down-vote bad ones. The rep penalty is, as described in the link you gave, to avoid excessive and irrational down-voting - not to stop you from doing it... (with all that said, of course I'm not trying to say you should down-vote this post. It was my own decision to do so. I just found your statement surprising) – Tomerikoo Dec 09 '20 at 16:50
1

You are using the while for both the limit and the counting which won't work.

You have to use the while for the limit and an if for the counting:

s = input()

i = 0
count = 0
while i < len(s):
    print(i)
    if "A" <= s[i] <= "Z":
        count += 1
    i = i + 1

print(f'Capitals in "{s}" = {count}')

However, this code is very complicated and better is the answer from @AlwaysSunny or the comment from @Samwise

quamrana
  • 37,849
  • 12
  • 53
  • 71
1

Your while loop is currently written such that it will terminate at the first lowercase letter. You need the loop to go over the entire string, but keep count of uppercase letters as it goes.

s = input()
i = 0
c = 0

while i < len(s):
    if "A" <= s[i] <= "Z":
        c = c + 1  # c only goes up on capital letters
    i = i + 1      # i goes up on every letter
    print(i, c)

print(f"Capital letters: {c}")

An easier method is to use the sum function along with isupper:

s = input()
print(f"Capital letters: {sum(c.isupper() for c in s)}")
Samwise
  • 68,105
  • 3
  • 30
  • 44
0
def Capital(In):
    return sum([l.isupper() for l in In])

print(Capital(input()))
Tomerikoo
  • 18,379
  • 16
  • 47
  • 61
Volta
  • 53
  • 9
-1

Try use this:

text = input()
count=0
for letter in text:
    if letter == letter.upper():
        count+=1
        print(letter, end=" ")
print(count-1)

I hope I have explained clearly)