![]() |
really new to python want to know how to do a loop - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: really new to python want to know how to do a loop (/thread-43266.html) |
really new to python want to know how to do a loop - pentopdmj - Sep-22-2024 want it so when you ask a number that inset 7 it asks you again what your number is and it keeps repeating like this print("Guess a number between 1 and 10:") number = int(input()) if number == 7: print("You got it!") elif number < 7: print("Higher") else: print("Lower") RE: really new to python want to know how to do a loop - menator01 - Sep-22-2024 Have a look at while loop A basic example from random import randint def random_number(): return randint(1, 10) number = random_number() while True: print('Guess a number between 1 and 10') try: question = int(input('>> ')) except ValueError: print('Please enter whole numbers only.') if question > number: print(f'{question} is too high.') elif question < number: print(f'{question} is too low.') else: print(f'Congradulations! You guessed {number}.') print('Would you like to play again? y/n') question = input('>> ').lower() if question == 'n': print('Goodbye!') break else: number = random_number() RE: really new to python want to know how to do a loop - imageflame - Mar-06-2025 while True: print("Guess a number between 1 and 10:") number = int(input()) if number == 7: print("You got it!") break elif number < 7: print("Higher") else: print("Lower") RE: really new to python want to know how to do a loop - Pedroski55 - Mar-08-2025 If you use a while loop, the player will have unlimited guesses, unless you put some kind of counter in there. You should use a for loop for the game and a while loop to repeat the game as many times as you wish. You can increase or decrease the number of guesses allowed. Python range works like this: range(start, stop, step) my_range = range(1, 21, 2) my_range.start # returns 1 my_range.stop # returns 21 my_range.step # returns 2 my_range.index(17) # returns 8 my_range.index(18) # produces an error because 18 is not in my_range """ Traceback (most recent call last): File "<stdin>", line 1, in <module> ValueError: 18 is not in range """These 2 for loops both produce the same result: for i in myrange: print(i) for i in range(1, 21, 2): print(i) randint(1,10) may turn out any of the numbers 1,2,3,4,5,6,7,8,9,10 which is a bit unusual in Python, because often the last number is not included, for example in a for loop.Using a while loop to loop over games and a for loop to play the game:from random import randint # function to play the game def gnum(num): print('The number to guess is somewhere in the range 1 to 10, inclusive.') print('You have 4 guesses. If you do not guess the number, you will receive an electric shock!') print('Resistance is useless, as is insulation!') print('Let the challenge begin ... ') for i in range(0,4): print(f'This is guess number {i+1} from a total of 4 guesses ... ') #print(f'num = {num}.') # the problem here is, when the input is not an integer, you will get an error # think of a way to check that! guess = int(input('Enter a number from 1 to 10. ')) if guess == num: print('You guessed right! No electric shock for you!') return elif guess < num: print(f'Your guess {guess} is too low.') elif guess > num: print(f'Your guess {guess} is too high.') print('O-oh! Get ready for 40 000 volts of taser!') answer = 'x' # keep playing the game until q is entered while not answer == 'q': answer = input('Enter anything, except q, to play. Enter q to quit. ') if answer == 'q': break else: num = randint(1, 10) # if you want to see the number to be guessed #print(f'num = {num}.') gnum(num)Shocking! RE: really new to python want to know how to do a loop - snippsat - Mar-08-2025 (Mar-08-2025, 07:57 AM)Pedroski55 Wrote: If you use a while loop, the player will have unlimited guesses, unless you put some kind of counter in thereNo,or you right if user🥴 don't follow hint lower or higher at all. The point is that user shall do a binary search strategy also name divide and conquer. So if number is 100 users shall be able to find number in 7 tries max,if follow this strategy. import random print("Guess a number between 1 and 100:") secret_number = random.randint(1, 100) while True: number = int(input()) if number == secret_number: print("You got it!") break elif number < secret_number: print("Higher") else: print("Lower") Here max 7. Of if more lucky it will be less that 7.
RE: really new to python want to know how to do a loop - Pedroski55 - Mar-09-2025 You are right! Using my psychic powers, I got it in 5! import random print("Guess a number between 1 and 1000:") secret_number = random.randint(1, 1000) while True: number = int(input("输入个数字 ")) if number == secret_number: print("You got it!") break elif number < secret_number: print("Higher") else: print("Lower") Not sure how that changes as the maximum number gets bigger! Doing it my way, I would have got an electric shock!
RE: really new to python want to know how to do a loop - snippsat - Mar-09-2025 (Mar-09-2025, 07:51 AM)Pedroski55 Wrote: Not sure how that changes as the maximum number gets biggerFor 1000 the max tries would be 10 if use optimal binary search strategy. Can make it better to make a function that take arguments,then is easier to change input. Eg for 5000 max tries would be 13 import random def guess_number_game(max_numb, max_tries): print(f"Guessing the number between 1 and {max_numb}.") secret_number = random.randint(1, max_numb) guess, tries = 0, 0 while guess != secret_number: guess = int(input("Take a guess: ")) tries += 1 if tries > max_tries: print(f"Used {tries} tries,max allowed for {max_numb} is {max_tries}. Game over!") print('Hint use a binary search strategy') print(f"The number was {secret_number}.") return if guess > secret_number: print("Lower") elif guess < secret_number: print("Higher") print(f'You guessed it! The number was {guess} in {tries} tries') if __name__ == '__main__': max_numb = 5000 max_tries = 13 guess_number_game(max_numb, max_tries)
|