虽然循环不能在Python中工作

时间:2017-06-22 00:43:19

标签: python loops while-loop

while循环无法正常工作。再次变量将决定是否将再次执行while循环。如果再次= 1,则执行while循环,程序将再次运行。如果再次= 0,则不会。

由于某种原因,再次= 1总是如此,所以无论如何,while循环总是被执行。有没有人注意到代码中的错误?

#include <stdio.h>
#include <string.h>

int strEndsWith(const char *str, const char *ending) {
    size_t len1 = strlen(str);
    size_t len2 = strlen(ending);
    return len1 >= len2 && !strcmp(str + len1 - len2, ending);
}

int main(void) {
    FILE *arm = fopen("C:\\Programming\\ARMENIA.TXT", "wt");
    FILE *ita = fopen("C:\\Programming\\ITALIA.TXT", "wt");
    FILE *esp = fopen("C:\\Programming\\ESPANIA.TXT", "wt");

    if (arm == NULL || ita == NULL || esp == NULL) {
        printf("Error: The archives could not be created.\n");
        return 1;
    } else {
        printf("Operation Successful: Archives created.\n\n");
    }

    for (;;) {
        char firstName[20];
        char lastName[30];
        char response;

        printf("\n\nPlease input the first name: ");
        if (scanf("%19s", firstName) != 1)
            break;
        printf("Please input the last name: ");
        if (scanf("%29s", lastName) != 1)
            break;

        if (strEndsWith(lastName, "ian")) {  
            fprintf(arm, "%s, %s\n", lastName, firstName);
        }
        if (strEndsWith(lastName, "ini")) {  
            fprintf(ita, "%s, %s\n", lastName, firstName);
        }
        if (strEndsWith(lastName, "ez")) {  
            fprintf(esp, "%s, %s\n", lastName, firstName);
        }

        printf("\n\nWould you like to enter another person into the archive?: (y) or (n): ");
        if (scanf(" %c", &response) != 1 || response != 'y')
            break;
    }

    printf("\n\nThe operation has finished.\n\n");

    fclose(arm);
    fclose(ita);
    fclose(esp);

    return 0;
}

2 个答案:

答案 0 :(得分:1)

在将值分配给名为的变量

的变量时,使用比较运算符
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
hwindow2 = FindWindow(vbNullString, "Trace")
main_view = FindWindowEx(hwindow2, 0&, "#32770", vbNullString)
System.Threading.Thread.Sleep(10)
sub_window = FindWindowEx(main_view, 0&, "Edit", vbNullString)
MessageBox.Show(main_view)
MessageBox.Show(sub_window)
Call SendMessage(sub_window, WM_SETTEXT, 0, "test")
End Sub

您必须删除其中一个等号:

again == int(input("Do you wanna go again?"))

答案 1 :(得分:0)

again == int(input("Do you wanna go again?"))

这不符合您的想法,因为==意味着它正在检查此陈述是否为真。你想要一个=。