Skocz do zawartości
  • 👋 Witaj na MPCForum!

    Przeglądasz forum jako gość, co oznacza, że wiele świetnych funkcji jest jeszcze przed Tobą! 😎

    • Pełny dostęp do działów i ukrytych treści
    • Możliwość pisania i odpowiadania w tematach
    • System prywatnych wiadomości
    • Zbieranie reputacji i rozwijanie swojego profilu
    • Członkostwo w jednej z największych społeczności graczy

    👉 Dołączenie zajmie Ci mniej niż minutę – a zyskasz znacznie więcej!

    Zarejestruj się teraz
  • 0

Pętla pracująca do określonego warunku


matrixsk2

Pytanie

Opublikowano

Witam, jak zrobić pętle, aby dało rade wpisywać liczbę dopóki się nie trafi w wylosowaną? Mój kod:

import random


print("Traf liczbę od 1 do 10.")


userGuess = input("Twoja liczba to: ")
userGuess = int(userGuess)

losowanie = random.randint(1,10)

if userGuess == losowanie:
print("Gratulacje!")
else:
print("Spróbuj ponownie.")

 

5 odpowiedzi na to pytanie

Rekomendowane odpowiedzi

Opublikowano

 

3 wynik z google.  :confused:

import random

print("\tWelcome to 'Guess My Number'!")
print("\nI'm thinking of a number between 1 and 100.")
print("Try to guess it in as few attempts as possible.\n")

# set the initial values
the_number = random.randint(1, 100)
guess = 0
tries = 0

# guessing loop
print(the_number)
while guess != the_number:

    try:
        guess = int(input("Take a guess: "))
    except ValueError:
        print("Please enter a numeric value")
        continue

    tries += 1

    if guess == the_number:
        print("You guessed it! The number was", the_number)
        print("And it only took you", tries, "tries!\n")
        break

    print("Lower...") if guess > the_number else print("Higher...")

input("\n\nPress the enter key to exit.")

Niepoprawny kod+ nie na temat

 

Poprawny kod:

import random


while True:
	print("Traf liczbę od 1 do 10.")
	losowanie = random.randint(1,10)
	while True:
		userGuess = int(input("Twoja liczba to: "))
		if userGuess == losowanie:
			print("Gratulacje!")
			break
		else:
			print("Spróbuj ponownie.")

1149730ee7ddaf7UVMV7ZB.gif

Opublikowano

3 wynik z google.  :confused:

import random

print("\tWelcome to 'Guess My Number'!")
print("\nI'm thinking of a number between 1 and 100.")
print("Try to guess it in as few attempts as possible.\n")

# set the initial values
the_number = random.randint(1, 100)
guess = 0
tries = 0

# guessing loop
print(the_number)
while guess != the_number:

    try:
        guess = int(input("Take a guess: "))
    except ValueError:
        print("Please enter a numeric value")
        continue

    tries += 1

    if guess == the_number:
        print("You guessed it! The number was", the_number)
        print("And it only took you", tries, "tries!\n")
        break

    print("Lower...") if guess > the_number else print("Higher...")

input("\n\nPress the enter key to exit.")

 

 

Zarchiwizowany

Ten temat przebywa obecnie w archiwum. Dodawanie nowych odpowiedzi zostało zablokowane.

×
×
  • Dodaj nową pozycję...