变量未被识别,我不确定为什么? [英] Variable isn't being recognised and I'm not sure why?

查看:140
本文介绍了变量未被识别,我不确定为什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

def errorgenerator():
    #Generates error messages and submits to the support team
    counter = open ("reports/counter.txt")
    counternum = int(counter.read())
    counternum = counternum + 1
    counter.close()


    counter = open ("reports/counter.txt", "w+")
    counter.write(int(counternum))
    counter.close()

    print("Apologies, we can not solve this issue. Please contact the supplier, to do this please leave your email to be further contacted by the supplier")
    email = input()

    print("Thank you, your error code is " +
    counter = open (("reports/report-" + int(counternum) + ".txt"), "w+")
    counter.write(email)
    counter.close() )



此代码应该创建一个带有counternum + 1的错误代码,然后将其写在用户电子邮件旁边的文件中。出于某种原因,代码将无法识别counter.write(email)中的counter。任何人都可以告诉我为什么或如何解决它?



我尝试过:



我曾尝试将反对数写为str和int,但无济于事。这是我唯一能想改变的事情。


This code is supposed to create an error code with counternum + 1 and then write that in a file alongside the user's email. For some reason, the code will not recognise the 'counter' in counter.write(email). Can anyone tell me why or how to fix it?

What I have tried:

I have tried writing the counternums as str and int but to no avail. Those were the only things I could think to change.

推荐答案

你不能把 int 写入文本流这条路。你应该把它转换成一个字符串,然后在输入上重新转换它,如:

You cannot write an int to a text stream this way. You should convert it to a string and then reconvert it on input like:
counter.write(str(counternum))

//
num = counter.read()
counternum = int(num) + 1
// write the updated value



此外,在编写更新的值时不要使用w +,否则最终会得到一个序列号文件。您可能会发现以二进​​制文件进行读写更好:请参阅 16.2。 io - 用于处理流的核心工具& mdash; Python 3.3.6文档 [ ^ ]。


Also, do not use "w+" when writing the updated value or you will just end up with a file of sequential numbers. You may find it better to read and write in binary: see 16.2. io — Core tools for working with streams — Python 3.3.6 documentation[^].




我建议你检查一下你的缩进。这是Python编程中的常见错误。



我在代码中看不到counter.write(email)表达式。



问候
Hi,
I'd recommend you check your indentation well. It's a common error in Python programming.

I can't see the "counter.write(email)" expression in your code.

Regards


这篇关于变量未被识别,我不确定为什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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