Reputation: 13
I am working on a code that is supposed to use a while loop to determine if the number inputted by the user is the same as the variable secret_number = 777. the following criteria are:
will ask the user to enter an integer number;
will use a while loop;
will check whether the number entered by the user is the same as the number picked by the magician. If the number chosen by the user is different than the magician's secret number, the user should see the message "Ha ha! You're stuck in my loop!" and be prompted to enter a number again.
If the number entered by the user matches the number picked by the magician, the number should be printed to the screen, and the magician should say the following words: "Well done, muggle! You are free now."
if you also have any tips how to use the while loop that would be really helpful. Thank you!
Upvotes: 0
Views: 6257
Reputation: 1
secret_number = 777
print(
"""
+================================+
| Welcome to my game, muggle! |
| Enter an integer number |
| and guess what number I've |
| picked for you. |
| So, what is the secret number? |
+================================+
""")
a = int(input("Enter here: "))
while a!= secret_number:
print("Ha ha! You're stuck in my loop!")
a = int(input("Try again: "))
print("Well done, muggle! You are free now.")
Upvotes: 0
Reputation: 1
I am able to achieve this using this code:
secret_number = 777
print(
"""
+================================+
| Welcome to my game, muggle! |
| Enter an integer number |
| and guess what number I've |
| picked for you. |
| So, what is the secret number? |
+================================+
""")
inp_number = int(input("Pick a number or Type 0 to stop "))
while inp_number != 0:
if inp_number != secret_number:
print("Ha ha! You're stuck in my loop!")
inp_number = input("Pick a number or Type 0 to stop ")
else:
print("Well done, muggle! You are free now.")
break
Upvotes: 0
Reputation: 1
We can do it using while true and if as follows:
secret_number= 777
number=int(input("enter an integer..."))
while True:
if number!= secret_number:
print(number, "Ha ha! You’re stuck in my loop")
#ask again
number=int(input("enter an integer..."))
else:
print("Well done muggle! You are free now.")
# here we need to break it as user has entered the secret_number
break
Upvotes: -1
Reputation: 1
Try this:
secret_number = 777
print(
"""
+================================+
| Welcome to my game, muggle! |
| I've picked a number for you. |
| So, what is the secret number? |
+================================+
""")
number = int(input("enter an integer..."))
# there's no need of if statement
while number != secret_number:
print("Ha ha! You're stuck in my loop!")
# ask again
number = int(input("enter an integer..."))
print("Well done, muggle! You are free now.")```
Upvotes: -1
Reputation: 93
Here are my examples:
Example 1:
secret_number = 777
guess_number = int(input("Enter a number: "))
while guess_number != secret_number:
print("Ha ha! You're stuck in my loop!")
guess_number = int(input("Enter a number: "))
print("Well done, muggle! You are free now.")
Example 2:
secret_number = 777
guess_number = int(input("Enter a number: "))
while True:
if guess_number == secret_number:
print("Well done, muggle! You are free now.")
break
else:
print("Ha ha! You're stuck in my loop!")
guess_number = int(input("Enter a number: "))
Upvotes: 0
Reputation: 27547
Here is how it's done:
secret_number = 777
n = int(input("Enter an integer number: "))
if n != secret_num: # If statement to chack if the numbers match
print("Ha ha! You're stuck in my loop!")
while True:
input("Enter an integer number: ") # Loop never ends...
print("Well done, muggle! You are free now.") # If the program made it here, that means the user's number matched
Output:
Enter an integer number: 7
Ha ha! You're stuck in my loop!
Enter an integer number: 2
Enter an integer number: 2
Enter an integer number: 3
Enter an integer number: 4
Enter an integer number: 56
Enter an integer number: 789
Enter an integer number:
Enter an integer number: 8765
Enter an integer number:
...
Round 2:
Enter an integer number: 777
Well done, muggle! You are free now.
>>>
Upvotes: 1
Reputation: 13049
You can escape from the loop using break
.
For example:
secret_number = 777
while True:
number = int(input("Enter an integer: "))
if number == secret_number:
break
print("Ha ha! You're stuck in my loop!")
print("Well done, muggle! You are free now.")
It is often better for sake of more comprehensible code to put the condition in the while
statement itself if possible, rather than break from the loop elsewhere. However, for this problem, the code needed to achieve that might require something like this, where the necessary restructuring of the code is probably not worthwhile compared to accepting the need to use break
:
def get_number():
return int(input("Enter an integer: "))
secret_number = 777
number = get_number()
while number != secret_number:
print("Ha ha! You're stuck in my loop!")
number = get_number()
print("Well done, muggle! You are free now.")
However, in python 3.8, the "walrus operator" (:=
) -- which allows a value to be simultaneously assigned to a variable and also used in an expression -- can be used to good effect to simplify the structure while avoiding the need for break
:
secret_number = 777
while (number := int(input("Enter an integer: "))) != secret_number:
print("Ha ha! You're stuck in my loop!")
print("Well done, muggle! You are free now.")
Of course, if you are not interested in storing the value, then you can just omit the use of the variable number
, and then a similar structure becomes possible without the need for :=
.
secret_number = 777
while int(input("Enter an integer: ")) != secret_number:
print("Ha ha! You're stuck in my loop!")
print("Well done, muggle! You are free now.")
Upvotes: 3
Reputation: 117
You can use while(true) to create a while loop. Inside, set a if/else to compare the value input and secret_number. If it's true, print("Well done, muggle! You are free now.") and break. Unless, print("Ha ha! You're stuck in my loop") and continue
Upvotes: 0