我目前正在编写python中的2个程序,这些程序必须相互进行数字游戏。一个程序选择1到100之间的数字。然后另一个程序尝试猜测该数字是什么。每当猜测者猜测时,选择者就会回答“太大”,“太小”或“你得到它”。根据答案,猜测者相应地调整了下一个猜测。
这是我选择的程序的代码:
import random
from guesser import g
guessCount = 0
number = random.randint(1, 100)
print("I'm thinking of a number between 1 and 100.")
outfile = open ('response.txt', 'w')
guess = 50
print (guess)
if guess < number:
print('Your guess is too low.')
switch = '1'
outfile.write (switch + '\n')
elif guess > number:
print('Your guess is too high.')
switch = '2'
outfile.write (switch + '\n')
else:
print('Correct, You guessed the number in', guessCount, 'guesses.')
switch = '3'
outfile.write (switch + '\n')
while guessCount < 8:
guess = g
print (guess)
guessCount += 1
if guess < number:
print('Your guess is too low.')
switch = '1'
outfile.write (switch + '\n')
elif guess > number:
print('Your guess is too high.')
switch = '2'
outfile.write (switch + '\n')
else:
print('Correct, You guessed the number in', guessCount, 'guesses.')
switch = '3'
outfile.write (switch + '\n')
break
outfile.close()
print('The number was',number)
以下是给出猜测的程序代码:
low = 1
high = 100
guess = 0
guessCounter = 0
infile = open ('response.txt', 'r')
switch = int (infile.readline())
def g (switch):
while switch != 3 and guessCounter < 8:
guess = (low+high)//2
guessCounter += 1
if switch == 1:
high = guess
elif switch == 2:
low = guess + 1
return guess
我的主要问题是如何让2个程序与彼此互动。我目前正在尝试使用一种让他们通过名为response的文本文件进行通信的方法,但肯定有一种更简单的方法吗?
主要问题我似乎是当选择器试图从guesser获取变量g时它不能,因为当前在response.txt中没有响应意味着switch = int(' “)
Traceback(最近一次调用最后一次):文件 “C:\ Users \ Jash \ Downloads \ guesser.py”,第8行,in switch = int(infile.readline())ValueError:基数为10的int()的文字无效:''
是的,它们必须是2个独立的程序。它必须在python中完成。
答案 0 :(得分:1)
将两个玩家放在同一个程序中要容易得多。
如果你真的想使用2,你可以在unix或linux上运行它们:
echo "" > somefile
tail -f somefile | program1 | program2 >> somefile
这将有效地将每个节目的输出传送到另一个节目的输入中。当然,任何你想看的东西都应该打印成标准错误。
答案 1 :(得分:0)
您可以从主脚本中打开子脚本,如下所示:
from subprocess import Popen, PIPE
prog = Popen("child.py", shell=True, stdin=PIPE, stdout=PIPE)
prog.stdin.write("Message to child.py maybe from another child?\n")
print prog.stdout.read() #Response from child
prog.wait() # Wait for script to finish run next script
答案 2 :(得分:0)
如Matt Timmermans所述: 如果不是绝对必要的话,把整个逻辑放到一个程序中:
import random
def createNum():
return random.randint(1,101)
lastGuess = 0
randMin, randMax = 1, 101
def guessNum(sigmoidAdjustmentInt):
# sigmoidAdjustmentInt represents a number which is negative, equal to zero or positiv; e.g. [-1 / 0 / +1]
# 0 == no information about to big or to small number
# -1 == number has to be smaller this time
# 1 == number has to be bigger this time
# guess somehow; e.g. with random again
if sigmoidAdjustmentInt < 0:
randMax = lastGuess-1
elif 0 < sigmoidAdjustmentInt:
randMin = lastGuess+1
return random.randint(randMin,randMax)
def main():
secretNumber = createNum()
guessedCorrectly = False
triesCounter = 0
sigmoidAdjustmentInt = 0 # set here for the first call
while not guessedCorrectly:
triesCounter = 0
if guessNum(sigmoidAdjustmentInt) == secretNumber:
guessedCorrectly = True
break
# print here if too high or low
print("needed guesses: "+ triesCounter)
# do something else
请注意,random.randint(...) - createNum和guessNum的调用只是您首选实现的占位符。
关于如何执行多个脚本的问题。 说你有3个文件:
a.py
b.py
c.py
您启动a.py
,它会执行某些操作,调用b.py
,然后调用c.py
并显示结果。
你可以这样做:
# in a.py
import subprocess
args = ( # rebuild the commandline call of the file here; the following stands for the comandline command: python b.py
"python", # every whitespace in the cmd-command is a comma in this args-tuple
"b.py"
)
popen = subprocess.Popen(args, stdout=subprocess.PIPE)
popen.wait()
resultsOfB, errorsOfB = popen.communicate()
del popen
args = ( # this represents: python c.py someStringValueContainedInResultFromB
"python",
"c.py",
resultOfB # lets just say this var contains a string => if not convert it to one
)
popen = subprocess.Popen(args, stdout=subprocess.PIPE)
popen.wait()
resultsOfC, errorsOfC = popen.communicate()
# do something with the results of c.py
再次:如果您编写所有三个文件,请考虑将它们合并为一个。如果你必须调用第三方软件或类似的东西(例如:)
,这种方法很好答案 3 :(得分:0)
创建第三个裁判计划。拿两个参数 - 选择器和猜测程序的名称。让裁判程序打开两个程序的读/写管道,并使用subprocess
或pexpect
模块。