简体   繁体   English

循环执行时遇到问题

[英]Having Trouble Executing While Loop

I am attempting to create a coronavirus temp scan compare program.我正在尝试创建一个冠状病毒临时扫描比较程序。 I want to open an excel file and create a small database each day of scanned employees.我想打开一个 excel 文件并创建一个每天扫描员工的小型数据库。 When I execute I am just getting the first input prompt and my excel spreadsheet is not opening.当我执行时,我只是得到第一个输入提示,我的 excel 电子表格没有打开。 My goal was to have each line of code under the while statement nested and would continue to loop until the program operator ended the loop.我的目标是让 while 语句下的每一行代码都嵌套并继续循环,直到程序操作员结束循环。 My code is below.我的代码如下。 Any help is appreciated.任何帮助表示赞赏。

from datetime import date
from xlwt import Workbook

# Workbook is created
wb = Workbook()

# add sheet
sheet1 = wb.add_sheet('Temperature Scans')

sheet1.write(0, 0, 'First Name')
sheet1.write(0, 1, 'Last Name')
sheet1.write(0, 2, 'Date')
sheet1.write(0, 3, 'Temperature')

Normal = 98.6

Recording = input("Are you Recording Temperature Today? 1 if yes; 0 if no: ")

while Recording == 1:
    Employee_First = input("Enter First Name: ")
    Employee_Last = input("Enter Last Name: ")
    Temp = float(input("Input Scanned Temperature (Example if 99 degrees enter 99): "))
    if Temp > Normal:
            print("Elevated Temperature Detected! Entrance Not Permitted")
    else:
            print("Temperature Within Acceptable Limits. Entrance Permitted")
    Date = today.strftime("%m/%d/%y")
    for i in range(0, 15000):
            sheet1.write(i+1, 0, Employee_First)
            sheet1.write(i+1, 1, Employee_Last)
            sheet1.write(i+1, 2, Date)
            sheet1.write(i+1, 3, Temp)

            Day = today.strftime("%d")
            Month = today.strftime("%m")
            Year = today.strftime("%y")

            wb.save(Month, _ , Day, _ , Year, 'Temp Scans.xlsx')

    break

    continue

Change this of your code:更改您的代码:

Recording = input("Are you Recording Temperature Today? 1 if yes; 0 if no: ")

To:至:

try:
    Recording = int(input("Are you Recording Temperature Today? 1 if yes; 0 if no: "))
except:
    print("Please enter a valid number (either 1 or 0)")

in order to make sure that you enter the while loop:为了确保您进入while循环:

...
while Recording == 1:
    Employee_First = input("Enter First Name: ")
...

Since the input() will always return a string variable and you are making the comparison on the while loop with an integer.由于input()将始终返回一个字符串变量,并且您正在使用 integer 在 while 循环上进行比较。

EDIT编辑

Here is a mofification of the code you provided using the openpyxl library.这是您使用openpyxl库提供的代码的修改版。 The code below will interactively ask for the employee's first name, last name and temperature and finally overwrite the temperaturescans.xlsx file (or create it if it doesn't exist and you run the python script for the first time).下面的代码将以交互方式询问员工的名字、姓氏和温度,最后覆盖temperaturescans.xlsx文件(或者如果它不存在并且您第一次运行 python 脚本,则创建它)。

from openpyxl import Workbook
from datetime import datetime

#Create workbook
wb = Workbook()

# grab the active worksheet
ws = wb.active

# Fill up the headers on the first row
ws['A1'] = 'First Name'
ws['B1'] = 'Last Name'
ws['C1'] = 'Date'
ws['D1'] = 'Temperature'

#Define some constant values
normal_temperature = 98.6
date = datetime.today().strftime('%m/%d/%y')

try:
    recording = int(input("Are you Recording Temperature Today? 1 if yes; 0 if no: "))
except:
     print('Please enter a number: either 1 or 0')

while recording == 1:
    #Get employees First Name
    employee_first = input('Enter First Name:')
    #Get employees Last Name
    employee_last = input('Enter Last Name:')
    #Get temperature
    try:
        temperature = float(input('Input Scanned Temperature (example if 99 degrees enter 99):'))
    except:
        print('Please enter a valid value for the temperature (99.2, 98.6)')
    #Check if the employee has a fever
    if temperature > normal_temperature:
        print('Elevated Temperature Detected! Entrance Not Permitted')
    else:
        print("Temperature Within Acceptable Limits. Entrance Permitted")
    #Add the employees row to the sheet
    ws.append([employee_first,employee_last,date,temperature])

    try:
        recording = int(input("Are you Recording Temperature Today? 1 if yes; 0 if no: "))
    except:
        print('All recordings have been made. Saving the file.')

#Save the file
print('Check the temperaturescans.xlsx file for the results.')
wb.save("temperaturescans.xlsx")

The input statement will only take input as a string, it is up to you to convert it into different formats:输入语句只会将输入作为字符串,您可以将其转换为不同的格式:

Recording = input("Are you Recording Temperature Today? 1 if yes; 0 if no: ")

while Recording == '1':
    # do something

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM