Python 中的 Break 语句之前的代码未执行
Code before Break Statement in Python not executed
我的程序是一个 Mastermind Game,抱歉我是个菜鸟:`)
这是我的代码:
def output(guess, number):
n=str(number)
g=str(guess)
p=""
if n==g:
p="Cracked!"
elif len(n) != len(g):
p="Enter a " + str(len(n)) + " digit number."
else:
for i in range (0,len(n)):
if g[i] == n[i]:
p=p+"✔"
elif g[i] in n:
p=p+"⭕"
else:
p=p+"✖"
return(p)
import random
turns=0
dig=int(input("Enter the number of digits you want to guess."))
low=10**(dig-1)
up=(10**dig)-1
number = random.randint(low, up)
print("Start Guessing!")
while 1==1:
guess=input()
out=output(guess, number)
print(out)
turns=turns+1
if guess==number:
print("You cracked the number in " + turns + " turns.")
break
这是我的预期输出:
Enter the number of digits you want to guess.3
Start Guessing!
123
✔✖✖
456
✖✖✖
789
✖✖✖
101
✔⭕⭕
110
Cracked!
You cracked the number in 5 turns.
但是,这是实际输出:
Enter the number of digits you want to guess.3
Start Guessing!
123
✔✖✖
456
✖✖✖
789
✖✖✖
101
✔⭕⭕
110
Cracked!
我的问题是,为什么break
之前的print("You cracked the number in " + turns + " turns.")
语句没有被执行?这是一些错误吗?或者有一些编码错误?请帮忙。
试试这个:
def output(guess, number):
n=str(number)
g=str(guess)
p=""
if n==g:
p="Cracked!"
elif len(n) != len(g):
p="Enter a " + str(len(n)) + " digit number."
else:
for i in range (0,len(n)):
if g[i] == n[i]:
p=p+"✔"
elif g[i] in n:
p=p+"⭕"
else:
p=p+"✖"
return(p)
import random
turns=0
dig=int(input("Enter the number of digits you want to guess."))
low=10**(dig-1)
up=(10**dig)-1
number = random.randint(low, up)
print("Start Guessing!")
while 1==1:
guess=input()
out=output(guess, number)
print(out)
turns=turns+1
if int(guess)==number:
print("You cracked the number in", turns, "turns.")
break
你混淆了类型,我们不可能说 "123"
等于 123
。
示例输出:
Enter the number of digits you want to guess.1
Start Guessing!
1
✖
2
✖
3
✖
4
✖
5
✖
6
✖
7
✖
8
✖
9
Cracked!
You cracked the number in 9 turns.
我的程序是一个 Mastermind Game,抱歉我是个菜鸟:`)
这是我的代码:
def output(guess, number):
n=str(number)
g=str(guess)
p=""
if n==g:
p="Cracked!"
elif len(n) != len(g):
p="Enter a " + str(len(n)) + " digit number."
else:
for i in range (0,len(n)):
if g[i] == n[i]:
p=p+"✔"
elif g[i] in n:
p=p+"⭕"
else:
p=p+"✖"
return(p)
import random
turns=0
dig=int(input("Enter the number of digits you want to guess."))
low=10**(dig-1)
up=(10**dig)-1
number = random.randint(low, up)
print("Start Guessing!")
while 1==1:
guess=input()
out=output(guess, number)
print(out)
turns=turns+1
if guess==number:
print("You cracked the number in " + turns + " turns.")
break
这是我的预期输出:
Enter the number of digits you want to guess.3
Start Guessing!
123
✔✖✖
456
✖✖✖
789
✖✖✖
101
✔⭕⭕
110
Cracked!
You cracked the number in 5 turns.
但是,这是实际输出:
Enter the number of digits you want to guess.3
Start Guessing!
123
✔✖✖
456
✖✖✖
789
✖✖✖
101
✔⭕⭕
110
Cracked!
我的问题是,为什么break
之前的print("You cracked the number in " + turns + " turns.")
语句没有被执行?这是一些错误吗?或者有一些编码错误?请帮忙。
试试这个:
def output(guess, number):
n=str(number)
g=str(guess)
p=""
if n==g:
p="Cracked!"
elif len(n) != len(g):
p="Enter a " + str(len(n)) + " digit number."
else:
for i in range (0,len(n)):
if g[i] == n[i]:
p=p+"✔"
elif g[i] in n:
p=p+"⭕"
else:
p=p+"✖"
return(p)
import random
turns=0
dig=int(input("Enter the number of digits you want to guess."))
low=10**(dig-1)
up=(10**dig)-1
number = random.randint(low, up)
print("Start Guessing!")
while 1==1:
guess=input()
out=output(guess, number)
print(out)
turns=turns+1
if int(guess)==number:
print("You cracked the number in", turns, "turns.")
break
你混淆了类型,我们不可能说 "123"
等于 123
。
示例输出:
Enter the number of digits you want to guess.1
Start Guessing!
1
✖
2
✖
3
✖
4
✖
5
✖
6
✖
7
✖
8
✖
9
Cracked!
You cracked the number in 9 turns.