Về cơ bản, đó là một trò chơi đoán và tôi thực sự có tất cả các mã ngoại trừ phần cuối cùng mà nó hỏi liệu người dùng có muốn chơi lại không. Làm cách nào để mã hóa điều đó, tôi sử dụng một vòng lặp trong thời gian đúng?
Đây là mã của tôi:
import random
number=random.randint[1,1000]
count=1
guess= eval[input["Enter your guess between 1 and 1000 "]]
while guess !=number:
count+=1
if guess > number + 10:
print["Too high!"]
elif guess < number - 10:
print["Too low!"]
elif guess > number:
print["Getting warm but still high!"]
elif guess < number:
print["Getting warm but still Low!"]
guess = eval[input["Try again "]]
print["You rock! You guessed the number in" , count , "tries!"]
while guess == number:
count=1
again=str[input["Do you want to play again, type yes or no "]]
if again == yes:
Guess = Eval [Input ["Nhập dự đoán của bạn từ 1 đến 1000"]]
if again == no:
break
Đã hỏi ngày 16 tháng 11 năm 2014 lúc 20:00Nov 16, 2014 at 20:00
4
Một vòng lặp lớn xung quanh toàn bộ chương trình
import random
play = True
while play:
number=random.randint[1,1000]
count=1
guess= eval[input["Enter your guess between 1 and 1000 "]]
while guess !=number:
count+=1
if guess > number + 10:
print["Too high!"]
elif guess < number - 10:
print["Too low!"]
elif guess > number:
print["Getting warm but still high!"]
elif guess < number:
print["Getting warm but still Low!"]
guess = eval[input["Try again "]]
print["You rock! You guessed the number in" , count , "tries!"]
count=1
again=str[input["Do you want to play again, type yes or no "]]
if again == "no":
play = False
Đã trả lời ngày 16 tháng 11 năm 2014 lúc 20:09Nov 16, 2014 at 20:09
AlualuAlu
7275 Huy hiệu bạc16 Huy hiệu Đồng5 silver badges16 bronze badges
2
Tách logic của bạn thành các chức năng
def get_integer_input[prompt="Guess A Number:"]:
while True:
try: return int[input[prompt]]
except ValueError:
print["Invalid Input... Try again"]
Ví dụ: để có được đầu vào số nguyên của bạn và cho trò chơi chính của bạn
import itertools
def GuessUntilCorrect[correct_value]:
for i in itertools.count[1]:
guess = get_integer_input[]
if guess == correct_value: return i
getting_close = abs[guess-correct_value] [number + 10]:
print["Too high!"]
elif guess < [number - 10]:
print["Too low!"]
elif guess > number:
print["Getting warm but still high!"]
elif guess < number:
print["Getting warm but still Low!"]
guess = int[input["Try again "]]
if guess == number:
print["You rock! You guessed the number in ", count, " tries!"]
return
guessNumber[]
again = str[input["Do you want to play again [type yes or no]: "]]
if again == "yes":
guessNumber[]
else:
sys.exit[0]
Ví dụ: để có được đầu vào số nguyên của bạn và cho trò chơi chính của bạn
Sau đó, bạn có thể chơi như
Bạn có thể đặt nó vào một vòng lặp để chạy mãi mãiNov 16, 2014 at 20:07
Joran Beasleyjoran Beasleyᔕᖺᘎᕊ
106K12 Huy hiệu vàng149 Huy hiệu bạc174 Huy hiệu đồng3 gold badges19 silver badges35 bronze badges
12
Đừng sử dụng eval
[như @icodex đã nói] - Đó là rủi ro, sử dụng
if again == no:
break
0. Một cách để làm điều này là sử dụng các chức năng:step=int[input['enter skip factor: ']]
num = int[input['Enter a number: ']]
while True:
for i in range[0,num,step]:
if [i % 2] == 0:
print[ i, ' is Even']
else:
print[i, ' is Odd']
again = str[input['do you want to use another number? type yes or no']
if again = 'no' :
break
pp_
Sử dụng các chức năng có nghĩa là bạn có thể sử dụng lại cùng một đoạn mã bao nhiêu lần bạn muốn.4 gold badges18 silver badges27 bronze badges
Tại đây, bạn đặt mã cho phần đoán trong một hàm gọi là
if again == no:
break
1, gọi hàm và cuối cùng, yêu cầu người dùng đi lại, nếu họ muốn, họ sẽ đi đến chức năng một lần nữa.Apr 1, 2016 at 19:44
0