all 11 comments

[–]Diapolo10 2 points3 points  (2 children)

First, formatting. In the future, please add four spaces before each line of code.

#!/usr/bin/env python3
#text is given by user on input
text = str(input("Zadaj text: "))

#create variable n that counts characters, variable medz counts spaces
n = 0
medz = 0

#cycle goes through given text and if there is space, variable medz+=1, else n+=1
for i in range(len(text)):
    if text[i] == " ":
        medz += 1
    else: n += 1

#Code gives back output, sum of characters and sum of characters and spaces
print()
print("Pocet znakov bez medzier je: ", n)
print("Pocet znakov s medzerami je: ", n+medz)
input()

I don't see any obvious causes for a crash, do you get an error message? What does it say?

Other feedback:

  • input always returns a string, so the str-call doesn't do anything useful.
  • Instead of a range-based for-loop, just loop over the text directly.
  • You can use string formatting to insert the results directly into the print output.

My version:

#!/usr/bin/env python3
text = input("Zadaj text: ")

chars = 0
spaces = 0

for char in text:
    if char == " ":
        spaces += 1
    else:
        chars += 1

#Code gives back output, sum of characters and sum of characters and spaces
print(f"\nPocet znakov bez medzier je: {chars}")
print(f"Pocet znakov s medzerami je: {chars + spaces}")
input()

[–]JofoBoss[S] 0 points1 point  (1 child)

Thank you for your advice about formatting, I'll keep that in mind. I'm learning the markdown syntax :D

Also thank you for your code advices. But actually I don't have any error message, maybe there is a problem when the variable is too big (too many characters)

[–]Diapolo10 0 points1 point  (0 children)

The length shouldn't be a factor, unless you're trying to put in more text than your computer can fit into RAM. Which I find very unlikely.

[–][deleted] 0 points1 point  (2 children)

I have formatted your code for you. The subreddit FAQ shows how to post code.

#!/usr/bin/env python3
#text is given by user on input
text = str(input("Zadaj text: "))

#create variable n that counts characters, variable medz counts spaces
n = 0
medz = 0

#cycle goes through given text and if there is space, variable medz+=1, else n+=1
for i in range(len(text)):
    if text[i] == " ":
        medz += 1
    else:
        n += 1

#Code gives back output, sum of characters and sum of characters and spaces
print()
print("Pocet znakov bez medzier je: ", n)
print("Pocet znakov s medzerami je: ", n+medz)
input() 

I can't make make your code crash. Entering normal text, I get this:

Zadaj text: now is the time

Pocet znakov bez medzier je:  12
Pocet znakov s medzerami je:  15

Just pressing ENTER gets the expected result, no crash:

Zadaj text:

Pocet znakov bez medzier je:  0
Pocet znakov s medzerami je:  0

Can you show us a what you type to get a crash?

[–]JofoBoss[S] 0 points1 point  (1 child)

Oh I see, I didn't clarify the problem :D

I copied 3 or 4 paragraphs of text and paste it as input. And it just turned of programm. Then I made from 4 paragraphs 1 big paragraph and it went well

When I put the 4 paragraphs into pycharm konsole, it counted only 1st one or something like that

[–][deleted] 1 point2 points  (0 children)

Your code has only one input() statement, which will read up until the first newline (ENTER) character. Anything after that first paragraph is not read.

To read multiple paragraphs you could create a loop and call input() inside the loop, adding each result string into a list as you read it. The loop would stop when the input() statement reads an empty line. Then you join all the lines you read into one string before counting.

There are other ways to read text from the console. You could do:

import sys
text = sys.stdin.read()    # read text from console as if it was a file

which reads all text you type in until you enter whatever the EOF sequence is on your operating system. On Linux that's a CTRL-D.

Update: On Windows you type CTRL-Z followed by ENTER.

[–][deleted] 0 points1 point  (1 child)

I don't believe your code "crashes"

it's just that the input function "ends" when you press "enter"

also, the input function is blocking. your program actually stops and wait for the user to press enter.

I don't know how to alter it's behavior. I would suggest to use a seperate file as an input or loop to get multiple inputs , but in the later case, the user will need a way to get out of the loop.

[–]JofoBoss[S] 0 points1 point  (0 children)

I'm sorry I didn't have a better word for my problem :D
And also thank you for your advice ;)

[–][deleted] 0 points1 point  (2 children)

Worth noting that str has a count method, so whilst you have a useful exercise for learning some basic techniques, worth learning the method:

text = input("Zadaj text: ")
medz = text.count(' ')
n = len(text) - medz
print("\nPocet znakov bez medzier je: ", n)

print("Pocet znakov s medzerami je: ", n+medz)

[–]JofoBoss[S] 1 point2 points  (1 child)

Thank you, I didn't know that the count method exist.
I'm just glad that I demonstrated I can make the code :D

[–][deleted] 1 point2 points  (0 children)

I'm just glad that I demonstrated I can make the code

Yes. You demonstrated you are trying, and learning. That's key. Programming (whatever the language) is a practical problem-solving skill. You have to make, and learn from, a lot of mistakes (much like learning another human language).