-1

I need to print input from the user in reverse order using a function. As a condition, just words (no floats/ int) should be allowed & at least four words need to be entered. e.g.: How can I help you --> you help I can How

It should'nt be possible to input: "4 5 6 7" or "There are 2 dogs" Here's my current code, however I didn't integrate that only strings are allowed so far:

def phrase():
    while True:
        user_input = input("Please insert a phrase: ")
        words = user_input.split(" ")
        n = len(words)
        if n >= 4:
            words = words[-1::-1]
            phrase_reverse = " ".join(words)
            print(phrase_reverse)
        else:
            print("Please only insert words and at least 4 ")
            continue
        break


phrase()

I tried with if n<4 and words == str:, if n<4 and words != string etc.. However, that didn't work. Could you please help me solving this issue? Maybe my code is wrong in general.

Näs
  • 3
  • 4
  • 1
    Please include the code in the body of your post, not an image.. – Justin Mar 24 '21 at 13:01
  • Alright, I just included the code. – Näs Mar 24 '21 at 13:04
  • You'll have to check each word. [Checking if a string can be converted to float in Python](https://stackoverflow.com/a/736050) – 001 Mar 24 '21 at 13:08
  • When asking on S/O It is *really very helpful* to show an example of what is input and how you want it output, especially if your code isn't working. It's also very helpful as an exercise when coding. – Konchog Mar 24 '21 at 13:12

2 Answers2

0

Reduce the problem to it's essential elements. The following should work - but your question looks suspiciously like homework.

    text = "test this for an example"  # use a text phrase to test.
    words = user_input.split().        # space is default for split.
    print(" ".join(reversed(words)))   # reverse list, and print as string.

result:

example an for this test

If you need to filter numerics..

    text = "test this for an 600 example" # use a text phrase to test.
    words = text.split()           # space is default for split.
    print(" ".join(reversed([wd for wd in words if not wd.isnumeric()])))
# filter, reverse, and print as string.

result:

example an for this test

If you need to reject numerics / responses with less than 4 words in a function.

def homework(text: str) -> bool:
    words = text.split()
    composed = list(reversed([wd for wd in words if not wd.isnumeric()]))
    result = len(words) == len(composed) and len(words) > 3
    if result:
        print(' '.join(composed))
    return result
Konchog
  • 1,920
  • 19
  • 23
  • I had a similar solution first. But I need to use "functions" and want to limit the input to "strings" (no numbers). How can I do that? – Näs Mar 24 '21 at 13:13
  • @Näs, I don't understand - do you want numbers rejected? As I say under your question, you need to show some example of what you want to happen. – Konchog Mar 24 '21 at 13:15
  • Yes numbers should be rejected. Example Input: How can i help you --> print: you help i can How. If the user enters numbers i want to ask the him for new input and not to include numbers – Näs Mar 24 '21 at 13:28
  • If you like it, then mark it as useful as well as being an answer! – Konchog Mar 24 '21 at 13:44
0

It is a little bit nasty, but might be useful for you to check for nums! :)

inp = input()

try:
    int(inp)
except ValueError:
    # do your operations here