เดาหมายเลขของคุณ
สคริปต์ง่ายๆที่ขอให้คุณนึกถึงตัวเลขระหว่าง 1 ถึง 63 จากนั้นจะเสนอรายการตัวเลขและถามว่าหมายเลขของคุณอยู่ในรายการนั้นหรือไม่ สามารถ refactored เพิ่มเติมได้หรือไม่? มีวิธีที่ดีกว่าในการสร้างรายการหรือไม่? การปรับปรุงใด ๆ ?
x= [xx for xx in range(1,64)]
num_i = [i for i in range(1,64,2)]
num_ii = x[1::16]+ x[2::16]+ x[5::16]+ x[6::16]+ \
x[9::16]+ x[10::16]+ x[13::16]+ x[14::16]
num_ii.sort()
num_iii= x[3::16]+ x[4::16]+ x[5::16]+ x[6::16]+ \
x[11::16]+ x[12::16]+ x[13::16]+ x[14::16]
num_iii.sort()
num_iv= x[7::16]+ x[8::16]+ x[9::16]+ x[10::16]+ \
x[11::16]+ x[12::16]+ x[13::16]+ x[14::16]
num_iv.sort()
num_v= x[15::32]+ x[16::32]+ x[17::32]+ x[18::32]+ \
x[19::32]+ x[20::32]+ x[21::32]+ x[22::32]+ \
x[23::32]+ x[24::32]+ x[25::32]+ x[26::32]+ \
x[27::32]+ x[28::32]+ x[29::32]+ x[30::32]
num_v.sort()
num_vi= [x for x in range(32,64)]
num_lists=[num_i,num_ii,num_iii,num_iv,num_v,num_vi]
addition_num= [1,2,4,8,16,32]
start_end= ["Think of a number between 1 and 63","I think the number you thought of was ..."]
intro= ["Is your number in the group of numbers below?",
"Is your number in this group of numbers ?","Is your number in this third group of numbers ?",
"Half way there. Is your number in this group of numbers ?",
"One more after this one. Is your number in this group of numbers ?",
"Last one. Is your number in this group of numbers ?"]
def format_list(xx):
for a,b,c,d,e,f,g,h in zip(xx[::8],xx[1::8],xx[2::8],xx[3::8],xx[4::8],xx[5::8],xx[6::8],xx[7::8]):
print('{:<8}{:<8}{:<8}{:<8}{:<8}{:<8}{:<8}{:<}'.format(a,b,c,d,e,f,g,h))
print('-'*62)
def main():
count=0
your_guess= 0
print(start_end[0])
ready= input('are you ready?(enter to quit or any key)')
if ready:
while count < 6:
print(intro[count])
format_list(num_lists[count])
reply = input("y/n ?")
if reply == "y" :
your_guess += addition_num[count]
count +=1
print(start_end[1] + str(your_guess))
replay= input('Play again? (enter to quit or any key)')
if replay:
main()
if __name__ == '__main__':
main()
คำตอบ
ตัวเลขมหัศจรรย์
ดังที่ได้กล่าวไว้แล้วในความคิดเห็นตัวเลข\$ 1, 2, \cdots \$ดูเหมือนจะสุ่มโดยสมบูรณ์สำหรับทุกคนที่ไม่แน่ใจว่าการสรุปจะเป็นอย่างไร (หรือควรจะ) การสร้างรายการยังยุ่งยากมาก เคล็ดลับเดียวกันนี้สามารถขยายเป็นการเลือกได้ไม่เกิน\$ 127, 255, 511, \cdots, 2^{n} - 1 \$.
เรียกซ้ำ main
คุณมีการโทรซ้ำmainในกรณีที่ผู้เล่นต้องการเล่นเกมซ้ำ การโทรซ้ำเมื่อคุณไม่กลับมาอาจทำให้เกิดสแต็กล้น ในสถานการณ์นี้การเรียกซ้ำไม่จำเป็นจริงๆอยู่แล้ว
รายการมากเกินไป
คุณมีเกือบทุกอย่างที่ใส่ไว้ในรายการ ซึ่งรวมถึงตัวเลขทั้งหมดจาก\$ 1-63 \$แผนภูมิที่สร้างขึ้นทั้งหมดจากตัวเลขเหล่านี้การรวมรายการข้างต้นทั้งหมดรายการพลังของ\$ 2 \$และอื่น ๆ
สำหรับบางคน tuple (ไม่เปลี่ยนรูป) เป็นสิ่งที่ดี แต่คนอื่น ๆ ก็ไม่จำเป็นต้องอยู่ในรายการอยู่ดี
การตั้งชื่อตัวแปร
x, xx, xxx, xxxx.. ไม่ได้มีความหมายจริงๆ ใช้ชื่อตัวแปรที่ดีกว่า
สำหรับการสร้างรายการตัวเลข:
for start in (2 ** i for i in range(6)):
l = [_ for _ in range(start, 64) if _ % (start * 2) >= start]
เขียนใหม่:
INTROS = (
"Is your number in the group of numbers below?",
"Is your number in this group of numbers?",
"Is your number in this third group of numbers?",
"Half way there. Is your number in this group of numbers?",
"One more after this one. Is your number in this group of numbers?",
"Last one. Is your number in this group of numbers?",
# "Or maybe the following?",
# "Add more intro strings here",
# "to get larger guessing range",
)
COUNT = len(INTROS)
UPPER_LIMIT = 2 ** COUNT
def display_chart(index: int, columns: int = 8):
print(INTROS[index])
start = 1 << index
chart = [str(x) for x in range(start, UPPER_LIMIT) if x % (start * 2) >= start]
for i in range(len(chart) // columns):
print("\t".join(chart[i * columns : (i + 1) * columns]))
def game():
guess = 0
print(f"Think of a number between 1 and {UPPER_LIMIT - 1}")
for current in range(COUNT):
display_chart(current)
reply = input("y/n? ")
if reply == "y":
guess += 1 << current
print(f"I think the number you thought of was ... {guess}")
def main():
while True:
game()
character = input("Play again? (enter to quit or any key)")
if not character:
break
if __name__ == "__main__":
main()
การเขียนซ้ำเป็นแบบไดนามิกโดยธรรมชาติในขณะนี้ หากคุณต้องการเพิ่มช่วงให้เพิ่มส่วนอื่นในINTROSทูเพิล
ผ่านครั้งแรก:
- ย้ายสิ่งที่สร้างรายการส่วนใหญ่ไปไว้ในฟังก์ชัน
- การแก้ไข PEP8: กำหนดช่องว่างให้เป็นมาตรฐานกำจัดการหลบหนีต่อเนื่องช่องว่างระหว่างฟังก์ชัน
- สิ่งที่ไม่เปลี่ยนรูปบางรายการแทนรายการ
def make_num_lists():
x = [xx for xx in range(1, 64)]
num_i = [i for i in range(1, 64, 2)]
num_ii = (x[ 1::16] + x[ 2::16] + x[ 5::16] + x[ 6::16] +
x[ 9::16] + x[10::16] + x[13::16] + x[14::16])
num_ii.sort()
num_iii = (x[ 3::16] + x[ 4::16] + x[ 5::16] + x[ 6::16] +
x[11::16] + x[12::16] + x[13::16] + x[14::16])
num_iii.sort()
num_iv = (x[ 7::16] + x[ 8::16] + x[ 9::16] + x[10::16] +
x[11::16] + x[12::16] + x[13::16] + x[14::16])
num_iv.sort()
num_v = (x[15::32] + x[16::32] + x[17::32] + x[18::32] +
x[19::32] + x[20::32] + x[21::32] + x[22::32] +
x[23::32] + x[24::32] + x[25::32] + x[26::32] +
x[27::32] + x[28::32] + x[29::32] + x[30::32])
num_v.sort()
num_vi = [x for x in range(32, 64)]
return [num_i, num_ii, num_iii, num_iv, num_v, num_vi]
num_lists = make_num_lists()
addition_num = (1, 2, 4, 8, 16, 32)
start_end = (
"Think of a number between 1 and 63",
"I think the number you thought of was ...",
)
intro = (
"Is your number in the group of numbers below?",
"Is your number in this group of numbers ?",
"Is your number in this third group of numbers ?",
"Half way there. Is your number in this group of numbers ?",
"One more after this one. Is your number in this group of numbers ?",
"Last one. Is your number in this group of numbers ?",
)
def format_list(xx):
for a,b,c,d,e,f,g,h in zip(xx[::8],xx[1::8],xx[2::8],xx[3::8],xx[4::8],xx[5::8],xx[6::8],xx[7::8]):
print('{:<8}{:<8}{:<8}{:<8}{:<8}{:<8}{:<8}{:<}'.format(a,b,c,d,e,f,g,h))
print('-'*62)
def main():
count = 0
your_guess = 0
print(start_end[0])
ready = input('are you ready?(enter to quit or any key)')
if ready:
while count < 6:
print(intro[count])
format_list(num_lists[count])
reply = input("y/n ?")
if reply == "y":
your_guess += addition_num[count]
count += 1
print(start_end[1] + str(your_guess))
replay = input('Play again? (enter to quit or any key)')
if replay:
main()
if __name__ == '__main__':
main()
แต่สิ่งนี้ไม่เพียงพอ ทำสิ่งที่ @ hjpotter92 แนะนำ