0

私はこのコードを持っています:

# -*- coding: utf-8 -*-
import random
import os, glob

guessesTaken = 0

print('Hello! What is your name?')
name = raw_input()
number = random.randint(1, 20)
print('Well, ' + name + ', I am thinking of a number between 1 and 20.')

while guessesTaken < 6:
    print('Take a guess.') 
    guess = raw_input()
    guess = int(guess)

    guessesTaken = guessesTaken + 1

    if guess < number:
        print('Your guess is too low.')

    if guess > number:
        print('Your guess is too high.')

    if guess == number:
        break

if guess == number:
    guessesTaken = str(guessesTaken)
    print('Good job, ' + name + '! You guessed my number in ' + guessesTaken + ' guesses!')

xa=len(name)
#has= str(name) + '    ' + str(guessesTaken) + 


f='toplevel.data'
with open("f", "a") as myfile:
    myfile.write('name')
    myfile.write(': ')
    myfile.write(name)
    myfile.write('          ')


    if len(name)>7:
        myfile.write((xa-7)*' ')
    elif len(name)==7:
        myfile.write(40*' ')
    else:
        myfile.write((7-xa)*' ')
    myfile.write('score')
    myfile.write(': ')
    myfile.write(guessesTaken)

次の問題があります。このコードの最後に、内容を含む toplevel.data を出力したいのですが、これは行いません。
ご協力ありがとう御座います。
#('gfdsgdgdfgdfgdfgdfgdfgsdgdfgdfgdfgdfgdfgfdgdfgdsfgfdgdfgdfgdfgdfgdfgdfgdfgdf')

4

2 に答える 2

1

まず、おそらく に置き換えたいと思うでしょwith open("f", "a") as myfilewith open(f, "a") as myfile。次に、スクリプトの最後に次を追加します。

with open(f) as myfile:
    print myfile.read()
于 2012-08-23T14:55:28.393 に答える
0

という名前のファイルに書き込んでいます"f"

with open(f, "a") as myfile:

また

with open('toplevel.data',"a") as myfile
于 2012-08-23T14:55:18.437 に答える