我如何完成这段代码? [英] How do I complete this piece of code?

查看:70
本文介绍了我如何完成这段代码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在制作一款名为Codebreaker的游戏。当用户猜出该号码时,生成一个4位数字。它应该在正确的位置输出正确的数字量(最多4个),并在错误的地方输出正确的数字(最多4个)。有12个生命,这是我到目前为止所做的



I am making a game called Codebreaker. A 4 digit number is generated while the user guesses the number. It should output the amount of correct digits (up to 4) in the correct place and amount of correct digits in the WRONG place (up to 4). There are 12 lives and this is what I have done so far

import random

code=[random.randint(1,9), random.randint(0,9), random.randint(0,9), random.randint(0,9)]
code=[2, 3, 6, 3]
display_answer= ["X","X","X","X"]
guesses= 1
corrPlace= 0
wronPlace= 0

instructions= input("Welcome to Codebreaker! Would you like to see the instructions type 'Yes' or 'No' ")
if (instructions == "Yes") or (instructions == "yes"):
  print("I will randomly generate a 4 digit number, try and guess it using your keyboard.")
else:
  print("Alrighty")


mode= input("What mode would you like to play on? 'Easy', 'Medium' or 'Hard': ")
mode= mode.lower()
if mode== "easy":
  print("Let's get on with it!" )

  print('\n')
  print("I am thinking of a four digit number, try and guess that number!")
  
  while True:
    print("You have: " , 13 - guesses, " guesses left")
    guess = input("Have a guess! Type 'quit' to quit: ")
  
    if guess == "quit":
      print('\n')
      print("You quit! The number I was thinking of was: ")
      print(code)
      print('\n')
      print("xX !YOU LOSE! Xx")
      break
  
    elif len(guess) != 4:
      print("The code is 4 digits long! Go again.")
      print('\n')
      continue

    elif guess.isdigit()== False:
      print("Please type in a NUMBER guess!")
      print('\n')
      continue

    if guesses == 12:
      print("You have no guesses left...")
      print("The number I was thinking of was: ", code)
      print("xX !YOU LOSE! Xx")
      break
    guesses = guesses+1

    guess1= int(guess[0])
    guess2= int(guess[1])
    guess3= int(guess[2])
    guess4= int(guess[3])

    if guess1== code[0]:
      display_answer[0]= guess1
    if guess2== code[1]:
      display_answer[1]= guess2
    if guess3== code[2]:
      display_answer[2]= guess3
    if guess4== code[3]:
      display_answer[3]= guess4
    
    print("You have guessed this part correctly:")
    print(display_answer, '\n')

    if display_answer[0] != "X" and display_answer[1] != "X" and display_answer[2] != "X" and display_answer[3] != "X":
      print('\n')
      print("You won! The number I was thinking of was: ")
      print(code)
      print('\n')
      print("xX !YOU WON! Xx")
      break

elif mode== "medium":
    ###
    print('\n')
    print("I am thinking of a four digit number, try and guess that number!")
    print (code)
    
    while True:
      print("You have: " , 13 - guesses, " guesses left")
      guess = input("Have a guess! Type 'quit' to quit: ")
    ###

    ###
      if guess == "quit":
          print('\n')
          print("You quit! The number I was thinking of was: ")
          print(code)
          print('\n')
          print("xX !YOU LOSE! Xx")
          break
    ###

    ###
      elif len(guess) != 4:
        print("The code is 4 digits long! Go again.")
        print('\n')
        continue
    ###

    ###
      elif guess.isdigit()== False:
        print("Please type in a NUMBER guess!")
        print('\n')
        continue
    ###

    ###
      if guesses == 12:
        print("You have no guesses left...")
        print("The number I was thinking of was: ", code)
        print("xX !YOU LOSE! Xx")
        break
      guesses = guesses+1
    ###

    ###
      guess1= int(guess[0])
      guess2= int(guess[1])
      guess3= int(guess[2])
      guess4= int(guess[3])
    ###

    ####
      corrPlace= 0
      if guess1==code[0]:
          corrPlace= (corrPlace+1)
      if guess2==code[1]:
          corrPlace= (corrPlace+1)
      if guess3== code[2]:
          corrPlace= (corrPlace+1)
      if guess4== code[3]:    
          corrPlace= (corrPlace+1)
    ####

    #####
      wronPlace= 0
      if guess1=! code[0]:
        if guess1== code[1] or code[2] or code[3]:
          wronPlace= (wronPlace+1)
        
      if guess2=! code[1]:
        if guess2== code[0] or code[2] or code[3]:
          wronPlace= (wronPlace+1)

      if guess3!= code[2]:
        if guess3== code[0] or code[1] or code[3]:
          wronPlace= (wronPlace+1)

      if guess4!= code[3]:
        if guess4== code[0] or code[1] or code[2]:
          wronPlace= (wronPlace+1)

      if corrPlace== 4:
        wronPlace= 0

    #####

    ###
      print ("You have guessed ", corrPlace, " digits in the correct place.")
      print ("But there are ", wronPlace, " correct digits in the wrong place.")
      print ('\n')

      if corrPlace== 4:
          print('\n')
          print("You won! The number I was thinking of was: ")
          print(code)
          print('\n')
          print("xX !YOU WON! Xx")
          break
    ###

elif mode== "hard":
  print ("Under construction")





问题在于:



The problem is with this:

#####
  wronPlace= 0
  if guess1=! code[0]:
    if guess1== code[1] or code[2] or code[3]:
      wronPlace= (wronPlace+1)

  if guess2=! code[1]:
    if guess2== code[0] or code[2] or code[3]:
      wronPlace= (wronPlace+1)

  if guess3!= code[2]:
    if guess3== code[0] or code[1] or code[3]:
      wronPlace= (wronPlace+1)

  if guess4!= code[3]:
    if guess4== code[0] or code[1] or code[2]:
      wronPlace= (wronPlace+1)

  if corrPlace== 4:
    wronPlace= 0





它应该输出最多四位数,但它模仿corrPlace变量我的计划为这段代码完成的是:



It should output up to four digits but it mimmicks the corrPlace variable. The plan I've done for this piece of code is this:

引用:

比较代码猜相同的数字

1到[0]

2到[1]

3到[2]

4到[3]



NOT开关



如果相同则1

如果不同则0



1 = 0

0 = 1



如果[1]匹配不比较(假)

如果[0]匹配不比较

如果[2]匹配不比较

如果[3]匹配不比较



如果[1]不匹配比较(TRUE)

如果[0]不匹配比较

如果[ 2]不匹配比较

如果[3]不匹配比较





如果与[1]相比较给予TRUE停止

如果为假携带

如果与[0]相比给予TRUE停止

如果虚假进行

如果与[2]相比给出TRUE停止

如果是伪进行

如果与[3]相比给出TRUE停止

如果误进行



TRUE = wronPlace + 1

4 = max

compare code to guess same number
1 to [0]
2 to [1]
3 to [2]
4 to [3]

NOT switch

if same then 1
if different then 0

1=0
0=1

if [1] matched dont compare (FALSE)
if [0] matched dont compare
if [2] matched dont compare
if [3] matched dont compare

if [1] not matched compare (TRUE)
if [0] not matched compare
if [2] not matched compare
if [3] not matched compare


if compared to [1] gives TRUE stop
if false carry on
if compared to [0] gives TRUE stop
if false carry on
if compared to [2] gives TRUE stop
if false carry on
if compared to [3] gives TRUE stop
if false carry on

TRUE= wronPlace+1
4 = max



请帮我改变这段代码到下面的计划中!我已经挣扎了一段时间



我尝试过的事情:



在计划之前,它使用while循环,如果statemts和导致失败的一切。尽管使用了我对布尔字段的了解并使用1和0(True和False)评估情况,我设法提出了基本计划。我预测continue关键字应该存在


Please help me change this piece of code into the plan below! I've struggles a while with this

What I have tried:

Before the plan it ried using a while loop, if statemts and everything that led to failure. I managed to come up with the basic plan though using my knowledge of the Boolean field and assessing the situation using 1 and 0 (True and False). I predict the continue keyword should be present

推荐答案

你的第一个问题是

Your first problem is that
if guess1== code[1] or code[2] or code[3]:



不是python。

更正是


is not python.
correction is

if guess1== code[1] or  guess1== code[2] or  guess1== code[3]:





您的问题是代码元素可以多次匹配猜测。您必须设置一个防止它发生的算法。



Your problem is that an element of code can match a guess more than once. You have to device an algorithm that prevent it to from happening.


这篇关于我如何完成这段代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆