要求用户输入 - 素数代码

时间:2016-12-07 09:29:50

标签: python python-3.x

' ----------------------------------------------------
' Main Test Function
' Debug - Boolean. Equals to false if running in [Test Mode] : reporting to Quality Center
' CurrentTestSet - [OTA COM Library].TestSet.
' CurrentTSTest - [OTA COM Library].TSTest.
' CurrentRun - [OTA COM Library].Run.
' ----------------------------------------------------
Sub Test_Main(Debug, CurrentTestSet, CurrentTSTest, CurrentRun)
  On Error Resume Next
  TDOutput.Clear

  ' Run Test
  strMainDir = "C:\Robot-QC"
  strTestDir = strMainDir & "\Execution"
  strBatchFileDir = strTestDir & "\BatchFiles"
  strResultDir = strTestDir & "\Results"

  strTestName = "TestCase1"
  strResultPath = strResultDir & "\" & strTestName & "_Res"
  strExecutionLogPath = strResultPath & "\ExecutionLog.txt"

  Set objFSO = CreateObject("Scripting.FileSystemObject")
  If objFSO.FolderExists(strResultPath) Then
     objFSO.DeleteFolder strResultPath, True
  End If
  objFSO.CreateFolder strResultPath

  'If Execution Directly From QC, Creates Test Batch File
  If Not objFSO.FileExist(strBatchFileDir & "\" & strTestName & ".bat")Then
     'Write Batch File for Test
     Set objTestBatchFile = objFSO.OpenTextFile(strBatchFileDir & "\" & strTestName & ".bat", 2, True)
     objTestBatchFile.WriteLine "cd " & strTestDir
     objTestBatchFile.WriteLine "pybot -t " & strTestName & " --outputdir Results\" & strTestName & "_Res " & "TestCases.txt"
     objTestBatchFile.Close
     Set objTestBatchFile = Nothing
  End If

  XTools.run strBatchFileDir & "\" & strTestName & ".bat", "", -1
  XTools.Sleep 1000

  ' Update Run Status
  strTDOutPut = Replace(TDOutput.Text, " ", "")
  If InStr(1, strTDOutPut, strTestName & "|PASS|") = 0 Then
     CurrentRun.Status = "Failed"
     CurrentTSTest.Status = "Failed"
  End If
  'Log Command Prompt Output
  Call Log_Output(objFSO,strExecutionLogPath)

  'Upload Result Files
  Set objFolder = objFSO.GetFolder(strResultPath & "\")
  Set objFiles = objFolder.Files

  For Each oFile In objFiles
       Set objRunAttach = CurrentRun.Attachments
       Set newAtt = objRunAttach.AddItem(Null)
       newAtt.FileName = strResultPath & "\" & oFile.Name
       newAtt.Type = 1
       newAtt.Post
       newAtt.Refresh
  Next

  Set newAtt = Nothing
  Set objRunAttach = Nothing
  Set objFiles = Nothing
  Set objFolder = Nothing
  Set objFSO = Nothing

  Err.Clear
  On Error Goto 0
End Sub

Sub Log_Output(objFSO,strExecutionLogPath)
   Set objOutputFile = objFSO.OpenTextFile(strExecutionLogPath, 2, True)
   objOutputFile.Write TDOutput.Text
   objOutputFile.Close
   Set objOutputFile = Nothing
End Sub

我的代码在这里找出了num部分的素数,我需要做的就是在运行代码时询问用户输入。

2 个答案:

答案 0 :(得分:0)

这非常简单:

num = int(input("Input a number: "))

答案 1 :(得分:0)

由于input()始终假定它正在使用字符串,因此您必须将其转换为int

print("Enter a number:")
num = int(input())   #this here
if num > 1: # if input is greater than 1 check for factors
   for i in range(2,num):
       if (num % i) == 0:
               print(num,"is not a prime number")
               break
   else:
       print(num,"is a prime number")
            #if input number is less than or equal to 1, it is not prime

else:
   print(num,"is not a prime number")
相关问题