Skocz do zawartości

Dołącz do społecznoś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

Pętla pracująca do określonego warunku

Rekomendowane odpowiedzi

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.")

 

Edytowane przez Vertisan

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.")

 

 

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.")

Edytowane przez Drenek

1149730ee7ddaf7UVMV7ZB.gif

Zarchiwizowany

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

Ostatnio przeglądający 0

  • Brak zarejestrowanych użytkowników przeglądających tę stronę.

Konto

Nawigacja

Skonfiguruj powiadomienia push w przeglądarce

Chrome (Android)
  1. Stuknij ikonę kłódki obok paska adresu.
  2. Stuknij Uprawnienia → Powiadomienia.
  3. Dostosuj swoje ustawienie.
Chrome (Komputer)
  1. Kliknij ikonę kłódki na pasku adresu.
  2. Wybierz Ustawienia witryny.
  3. Znajdź Powiadomienia i dostosuj swoje ustawienie.