Python,用多个参数写入txt

时间:2016-04-27 13:21:52

标签: python python-3.x

我有这个程序要求用户完成10个数学问题,每次他们得到正确的答案,他们的分数就会增加1分。

current_class = Classes[student_class]
class_score = open(current_class, 'r+')
class_format = (name, "has a score of", Score)
class_score.write(class_format)
print(class_score.read())

但是,当我读取.txt文件时,它会返回错误

TypeError: must be str, not tuple

我想要打印:

 Imran has a score of 10

3 个答案:

答案 0 :(得分:2)

class_format应该是:

class_format = "{} has a score of {}".format(name, Score)

检查docs 7.1.3.1 部分)

部分

答案 1 :(得分:0)

使用str.join从元组构建字符串。

class_format = (name, "has a score of", Score)
class_score.write(" ".join(class_format))

答案 2 :(得分:0)

你可以,

(define blogPost%
  (class object% ; object% instead of horizontal-panel%
    ; This argument is explicit now.
    ; If other code relies on other arguments, specify them here.
    (init parent)
    (super-new)
    (define panel
      (new horizontal-panel% ; this new call is explicit now
           [parent parent]   ; you can later add more arguments
           [alignment '(left top)])) ; and it won't break things
    (define titleoutput
      (new text-field%
           [label "    title"]
           [min-height 20]
           [min-width 200]
           [vert-margin 20]
           [horiz-margin 10]
           [parent panel])) ; panel instead of this
    (define output
      (new text-field%
           [label "blog"]
           [style '(multiple)]
           [min-height 20]
           [vert-margin 20]
           [min-width 400]
           [parent panel])) ; panel instead of this
    (define (callback button event)
      (define title-new-value (send titleoutput get-value))
      (define new-value (send output get-value))
      (save title-new-value new-value)
      (send output set-value "")
      (send titleoutput set-value "")
      (send howisit show #t))
    (define button
      (new button%
           [label "Submit"]
           [vert-margin 0]
           [horiz-margin 10]
           [parent panel] ; panel instead of this
           [callback callback]))
    ))

(define f (new frame% [label "blog post GUI"] [min-width 400] [min-height 500]))

(define tib (new blogPost%
                 [parent f]))

(send f show #t)

blogPost%

这样你也可以做到以下几点,

http://localhost:8080/feasthunt/changePassword.html?TOKEN=0FA3267F-0C62-B1C9-DB71-76F6829671ED

我更喜欢第二个。