3

I need to check whether the input is empty or not and cannot use if statements.

print("What is your name?")
name = input()
print("Hi, {}".format(name))
5
  • You could use try and except statements, or use while to see if length of input is not zero. Commented Mar 8, 2022 at 1:09
  • 1
    Who is saying you cannot use if statements? Commented Mar 8, 2022 at 1:16
  • What do you need to do if the input is empty? Commented Mar 8, 2022 at 1:17
  • I need to implement a program that ask for user's name, after getting user's name , print "Hi, user's name", if user did not put in a name or put in a empty name, then should print "You didn't key in any name" Commented Mar 8, 2022 at 1:22
  • Do you need to stop the code if the person does not key in any name? Commented Mar 8, 2022 at 1:45

4 Answers 4

1

Use a while loop that only terminates if the length of name is 0:

name = ""
while len(name) == 0:
    print("What is your name?")
    name = input()
print("Hi, {}".format(name))
Sign up to request clarification or add additional context in comments.

2 Comments

while not name: works too
Agree, but I think this syntax is more intuitive for people that don't know about truthiness/falsiness.
0

You could try something like this:

print("What is your name?")
name = input()

name = "Hi, {}".format(name)
while name == "Hi, ":
    name = "You didn't key in any name"

print(name)

This is ugly, but it will produce the exact output you want.

The idea is to use a loop that will run only once, and will only run if the name variable is empty after input() is called.

7 Comments

This is basically a glorified if statement.
@EpicProgrammer yes, but usually when someone gives a task of "don't use common operator X" the idea is to see how the programmer coerces another operator to do the same thing. If the OP was using Python 3.10 this could probably be done with pattern matching instead...
This is why I think these kinds of assignments are useless, because operators exist to do an option. And if/while statements are in different categories anyways, since one is for looping and one is for boolean conditions. Programming doesn't work well without the if statement, since that's how computers make decisions.
@EpicProgrammer as someone who actually had to interview potential developers, these kinds of questions are important, because they demonstrate the ability of a programmer to solve problems, not just use tools in a standard manner. It also shows understanding of the language, though usually these kinds of questions are better formed than the one OP posted. Also, at the assembly level there is no if, that is a high level imperative language construct and programming can work fine without it. functional languages often try to avoid using it.
assembly still has the cmp instruction. Maybe it's not the same as a full if statement but it allows the program to do some conditional logic. Using a singular-turn while loop for conditional logic is very stupid.
|
0

I would recommend assert. It will stop your programming to continue running when the requirement is not met.

print("What is your name?")
name = input()

assert name != "", "You didn't key in any name"
print("Hi, {}".format(name))


What is your name?

AssertionError: You didn't key in any name

2 Comments

You need to put the assert statement before the print("Hi part, otherwise Hi will always be printed (this does not match you output example.
Thanks for noticing it, I will fix it.
0

As my previous answer with the while loop received some criticism, I decided to demonstrate a less "naive" but perhaps more complicated solution, that actually does not use any kind of direct conditional operator:

print("What is your name?")
name = input()

answer = {}
answer[len(name)] = "Hi, {}".format(name)
answer[0] = "You didn't key in any name"


print(answer[len(name)])

Here we rely on a dictionary with the length of the input as an integer key.

We don't even need to compare the length to 0, we just overwrite the 0 key with the error message.

If input length is greater than 0, the name will be under its own key, and will be printed, if not, the empty "Hi" string will be replaced.

Would this ever be useful in the real world?
Probably not, unless there are many more than 2 options.

Does it comply with the task requirements?
Yes. It gives the desired output.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.