If I understand correctly, the computer randomly chooses numbers and the player should guess whether the next number is going to be higher or lower than the current one, right?

You could do something like this:

import random
def play():
print("Welcome by the higher lower game!")
next_number = random.randint(1, 100)
while True:
current_number = next_number
next_number = random.randint(1, 100)
print("This is the current number:", current_number)
player_answer = input("Will the next number be higher or lower? Answer here: ")
if not player_answer:
print("Bye!")
break
elif (player_answer == "higher" and next_number >= current_number or
player_answer == "lower" and next_number <= current_number):
print("Well done!")
else:
print("Nope!")

If I misunderstood the game and it is the classical programming exercise where one number is chosen randomly and the player should guess it using indications that it is lower or higher than each guess, then this might do the trick:

import random
def play2():
print("Welcome by the higher lower game 2!")
answer = random.randint(1, 100)
print("I have chosen a number between 1 and 100. Try to guess it!")
tries = 0
while True:
guess = int(input("Enter number: "))
tries += 1
if guess < answer:
print("Higher!")
elif guess > answer:
print("Lower!")
else:
print(f"Congratulations! You guessed my number in {tries} tries.")
break

Yeah, while True: can be a bit confusing at first. The expression that has to be true for the loop to continue is… well, True, which means that the loop continues forever. Unless you break it.

You could write it like this instead:

import random
def play():
print("Welcome by the higher lower game!")
next_number = random.randint(1, 100)
continue_game = True
while continue_game:
current_number = next_number
next_number = random.randint(1, 100)
print("This is the current number:", current_number)
player_answer = input("Will the next number be higher or lower? Answer here: ")
if not player_answer:
print("Bye!")
continue_game = False
elif (player_answer == "higher" and next_number >= current_number or
player_answer == "lower" and next_number <= current_number):
print("Well done!")
else:
print("Nope!")

The idea is that we always want to enter the while loop at least once. The flow of a while is this:

Test that the condition is true
If it is, execute the block
Test again that the condition is true
If it is, execute the block
etc.
When it becomes false, move forward.

Some other languages have a do { ... } while ... statement that starts by executing the block once and only then test the condition.

Execute the block.
Test the condition.
If it is true, execute the block again.
etc.
When the condition becomes false, move forward.

Python doesn’t have this. The approach taken is different. You make a while loop that has True as a condition. It runs indefinitely, until you use the break statement to move out of it. Is that clear?

I understand your vision and the think about. But somehow i keep getting errors.

import random
def play():
print(“Welcome by the higher lower game!”)

next_number = random.randint(1, 100)
continue_game = True
while continue_game:
current_number = next_number
next_number = random.randit(1, 100)
print("This is the current number:", current_number)
player_answer = input("Will the next number be higher or lower? Answer here: ")
if not player_answer:
print("Bye, next time better!")
continue_game = False
elif (player_answer == "higher" and next_number >= current_number or
player_answer == "lower" and next_number <= current_number):
print("Well done, next level!")
else:
print("nope")

can you somehow explain me now how the terminal nothing print?

again thanks for the help/ i appreciate the help. i just started to understand python. i have some pragrammer experience with PLC’s. but this is a differtent kind.

This is a common form when you can’t know the condition ahead of the
loop.

In your situation you’re doing something until a particular situation
occurs. So the logic often reads “forever, do something and stop on
success”. “forever” is often written “white True:”, because that
condition (“True”) is always true and so the loop runs forever unless
you take an extra action to leave the loop.

So you will see things like this:

while True:
n = int(input("give me a number, enter 0 to exit the loop"))
if n == 0:
# leave the loop
break
... do something with n ...

which would ask for a number forever until the user enters “0”. The
other way to write this loop might look like this:

terminated = False
while not terminated:
n = int(input("give me a number, enter 0 to exit the loop"))
terminated = n == 0
if not terminated:
... do something with n ...

Before the loop, although we have never tested the value of “n”, we can
pretend that it was not the loop termination value. Then enter the loop.

The cpu has to choose randomly a number before the answer of the player.and the cpu has to check if the number higher or lower than the first number. and so on.

if i understand you correctly is you code the right way to go for it if i want that.

is it possible what i want?

welcome at the game. (cpu shows a randomly number between 1 -100)

cpu choose a next number randomly( in secret.)

players gives answer

cpu check if the secret number higer or lower is than the first number.

what exactly mean you Cameron?
The cpu has to choose randomly a number before the answer of the
player.and the cpu has to check if the number higher or lower than the
first number. and so on.

if i understand you correctly is you code the right way to go for it if i want that.

is it possible what i want?

welcome at the game. (cpu shows a randomly number between 1 -100)

This above happens before the loop.

This stuff below happens inside the loop:

cpu choose a next number randomly( in secret.)

players gives answer

cpu check if the secret number higer or lower is than the first number.

cpu check if the answer correct(then next level)

if it wrong start again.

If 2 through 5 are inside the loop, 6 is implicitly done by hitting the
bottom of the loop. To do “not 6 (answer correct)”, you break out of the
loop.

So:

1
while True:
2
3
4
5 correct = (test the answer here)
6 if correct: break

this is the message i receive.
and this is the code.

import random
def play():
print(“Welcome by the higher lower game!”)

next_number = random.randint(1, 100)
continue_game = True
while continue_game:
current_number = next_number
next_number = random.randint(1, 100)
print("This is the current number:", current_number)
player_answer = input("Will the next number be higher or lower? Answer here: ")
if not player_answer:
print("Bye, next time better!")
continue_game = False
break
elif (player_answer == "higher" and next_number >= current_number or
player_answer == "lower" and next_number <= current_number):
print("Well done, next level!")
else:
print("nope")

I wrapped my code in a function without realizing you didn’t know about these, sorry. If you don’t want them, flatten it.

import random
print("Welcome by the higher lower game!")
next_number = random.randint(1, 100)
continue_game = True
while continue_game:
current_number = next_number
next_number = random.randint(1, 100)
print("This is the current number:", current_number)
player_answer = input("Will the next number be higher or lower? Answer here: ")
if not player_answer:
print("Bye, next time better!")
continue_game = False
break
elif (player_answer == "higher" and next_number >= current_number or
player_answer == "lower" and next_number <= current_number):
print("Well done, next level!")
else:
print("nope")

And you won’t need play(), the code will execute directly.