-3

My problem is really frustrating. I'm learning Python right now and can't find a solution for my problem. This is only an example for different outputs. not a code with usage.

here's my Code:

text_2 = input("Input number or text: ")

if   text_2 == --number-- :

         print("Your number is: ")

elif text_2 == --text aka string-- :

         print("Your text is: ")

else:
         print("Wrong_Input_1")

How can I do, that python recognizes the difference between a number and a string aka letters. my first problem was with

if text_2 == str(input) :
      print("Your IP is: ")

Python interprets even int's as string

my second problem: I don't know how Python only takes int as input. My thought was:

if text_2 == int(input) :

      print("Your IP is: ")

But that's not working.

the last problem is:

I want something similar to ´goto´. I know it's different in Python and not recommended to do it. so I need something different. In the following code I will show how it should be like


first_stage

text_2 = input("Which stage will you go?: ")

if   text_2 == "go to second stage" :

         print("going to second stage")
            goto second_stage

elif text_2 == "go to third stage" :

         print("going to third stage")
            goto third_stage


elif text_2 == "go to first stage" :

         print("going to first stage")
            goto first_stage

second_stage

text_3 = input("Which stage will you go?: ")

if   text_3 == "go to first stage" :

         print("going to first stage")
            goto first_stage

elif text_3 == "go to second stage" :

         print("going to second stage")
            goto second_stage


elif text_3 == "go to third stage" :

         print("going to third stage")
            goto third_stage

third_stage

text_4 = input("Which stage will you go?: ")

if   text_4 == "go to third stage" :

         print("going to third stage")
            goto third_stage

elif text_4 == "go to first stage" :

         print("going to first stage")
            goto first_stage


elif text_4 == "go to second stage" :

         print("going to second stage")
            goto second_stage

Would be nice if you can give me the alternative code for my last problem right in my code.

Last question: which command do I need to clear the output-window?

Jason Aller
  • 3,541
  • 28
  • 38
  • 38

3 Answers3

0

To address your second problem, you need to use a = int(input()) to get an input of type int for a. If you would like to keep it as a string, simply use a = input() and that will give you an input of type string for a. To clear the output window, this question requires more clarity. If you're on a Mac, in your terminal just type clear. More information can be found here: https://www.quora.com/Is-there-a-Clear-screen-function-in-Python

alpharoz
  • 119
  • 9
0

Woof. There's a lot to unpack here.

First of all, here's another question about validating IP addresses in Python.

And here's one about domains.

About the goto statements. Most modern languages do not use goto. Python is included. Instead they use the following constructs known as control statements.

while
if
for
switch (but not in python)

Another important construct is known as a function.

That being said, due to this awesome and weird project you are allowed to completely disregard how the language is intended to be used, and use goto anyway. So there's that.

BTW on mac and maybe linux, you can press CMD-K (CTRL-K) while in a python terminal and it will successfully clear the screen. (In fact it'll clear any command line application).

Samie Bencherif
  • 1,285
  • 12
  • 27
  • It's not actually about IP's and domain.. I didn't found an better example where its important to differentiate between an int input and an str input... and how do I would use while and for in my code? – Awesome Sounds Dec 06 '18 at 04:08
0

As requested, here is some information about how to go from using goto's to using "modern" constructs. I'm writing in pseudo code with the exception that control structures will be written in pythonic form.

Control structures are basically shortcuts that replace all the common (and necessary) ways of using goto (jmp) instructions.

If Statements

if notHungry goto skip
Eat
skip:
Sleep

Becomes

if not notHungry:
    Eat
Sleep

Notice how indention is used to define a code block. Code blocks remove the need for labels that skip sections of code.

While Loop

c = 0
repeat:
c = c + 1
Say Hello
if c < 100 goto repeat

becomes

while c < 100:
    c = c + 1
    Say Hello

For Loop

c = 0
repeat:
c = c + 1
Say Hello
if Happy goto repeat
Give Cookie
if LateForSchool goto skip
if c < 1000 goto repeat
skip:

becomes

for c in range(1000):
    Say Hello
    if Happy:
        continue
    Give Cookie
    if LateForSchool:
        break

Functions

Finally, and arguably most importantly is functions.

goto skip
doALotOfWork:
line1
line2
line3
line4
line5
ret
skip:

Prepare for Work
goto doALotOfWork
Do something else
goto doALotOfWork

becomes

def doALotOfWork():
    line1
    line2
    line3
    line4
    line5

Prepare for Work
doALotOfWork()
Do something else
doALotOfWork()

You can even pass information in and out of functions. (first runnable example)

def square(x):
    return x*x

print(square(2))

The information flows like this:

def square(informationGoingIntoFunction):
    return informationComingBack

print(square(informationGoingIntoFunction))

and it prints informationComingBack

Samie Bencherif
  • 1,285
  • 12
  • 27
  • But how do I do it, if the code should skip several lines of code? (e.g. 5.000 lines or more) can you give me the example at my big code with the stages? it's for me way eaysier to understand. – Awesome Sounds Dec 06 '18 at 04:50
  • I don't understand your example. What does it do? @AwesomeSounds – Samie Bencherif Dec 06 '18 at 05:12
  • You can imagine my code as text-adventure..You do an action (input) and then a different text show up. but you have more than one possibility.. so you must be able to get to a totally different line in the code – Awesome Sounds Dec 06 '18 at 05:37
  • I think text adventure is easiest to do using object orientation.. You can have stuff like `stage2 = Stage("this is stage 2");stage1 = Stage("this is stage 1"); stage1.option('go to second stage', stage2)`. Again when it comes to translating from goto to modern paradigm it's really a case by case thing. But it is in fact mathematically provable that you never "need" goto statements. – Samie Bencherif Dec 06 '18 at 06:05
  • I think... ... I don't get it... but let me google "object orientation python" – Awesome Sounds Dec 06 '18 at 06:11
  • OK. Also I made [some code for your example](https://repl.it/@SamyBencherif/Stage-Game-No-GOTO). Hope it helps. Is there anything in particular you are trying to make? – Samie Bencherif Dec 06 '18 at 06:19
  • 1
    wow :D its working ^^ and now a cup of coffee and studying the code thanks – Awesome Sounds Dec 06 '18 at 06:40