With Python, I want to start with a number 1 as an input, and then I want to make a game of 369 so that the computer outputs a number or a pair for every number that goes into it. Also, I want to make the 33rd, 36th, and so on. Masters, please help me.
python
The computer is a C++-based 369 game that you created on the premise that you always enter the correct answer.
It can be inefficient because it is easily written according to the flow of consciousness.
However, the program works normally for the purpose.
Please refer to the code below and change it to fit Python grammar.
/*
<Game Over Conditions >
(1) If you enter a number incorrectly
(2) If you enter a number when you need to enter a pair
(3) You must enter a number, but if you enter a pair,
(4) The number of inputs in the 'pair' does not match the correct answer
< Input Exception Conditions >
- If you enter all characters or strings except for non-numeric 'pair' (omitted)
*/
---
/*
* author: anonymous
* * date: 06/01/2021 (m/d/y)
* * purpose: QA answer
*/
#include <iostream>
#include <vector>
using namespace std;
void comInput(intn); // A function that outputs a computer input value (a value that is added to the user's input value is passed as a factor)
void printResult (const string & reason, answer); // a function that outputs results (outputs the number of times matched with the reason for ending the game)
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int user, answer;
string s_user, s_answer, s_tmp;
user = answer = 1;
while (true)
{
cout << "-------------------" << '\n';
s_answer = "";
s_tmp = to_string(answer); //Split the correct value by digit through type conversion and store it in a string variable
For (char c : s_tmp) // Store each digit of the correct answer stored in the string temporary variable repeatedly in the single character variable c
{
int x = c - '0'; // Convert a single character c to a single-digit integer and store it in the integer variable x
If (x!=0 &&x%3 == 0) //x is a multiple of 3, store 'pair' in s_answer (cumulative by repeating the for-each statement)
{
s_answer += "even";
}
}
If (s_answer.empty()) //s_answer is empty means that there is no multiple of 3 for each digit (=the answer is integer)
{
cout << "User input: ";
cin >> user;
If (cin.fail()) // if input fails (if text or string is entered) End game
{
printResult ("invalid input"), answer);
break;
}
If (user == answer) //if correct, call computer input function
{
comInput(answer + 1);
}
else //if incorrect, exit game after incorrect answer output
{
printResult ("wrong"), answer);
break;
}
}
If there is one or more multiple of 3 in each digit of else //s_answer (= correct is a character or string)
{
cout << "User input: ";
cin >> s_user;
If (cin.fail()) //Failed (if you entered a number) End the game
{
printResult ("invalid input"), answer);
break;
}
If (s_user == s_answer) //if correct, call computer input function
{
comInput(answer + 1);
}
else //if incorrect, exit game after incorrect answer output
{
printResult ("wrong"), answer);
break;
}
}
answer += 2; //The next character (integer or string) that the user enters will skip the computer input, so add 2
}
return 0;
}
void comInput(intn) { //A function that outputs a value that the computer will enter (the next number of correct answers entered by the user is taken as a factor)
cout << "Computer input: ";
strings_tmp = to_string(n); // Split the received factor value by digit through type conversion and store it in a string variable
strings_answer; // declare string variable to accumulate 'pair'
For (char c : s_tmp) //split the stored numbers one by one in a single character c
{
int x = c - '0'; // Convert a single character c to a single-digit integer and store it in the integer variable x
If (x!=0 &&x%3 == 0) //x is a multiple of 3, store 'pair' in s_answer (cumulative by repeating the for-each statement)
{
s_answer += "even";
}
}
If (s_answer.empty()) //s_answer is empty, it means that 3 or 6 or 9 is not included in the received factor, so output the factor as a number
{
cout << n;
}
else //if not, output s_answer with accumulated 'pair'
{
cout << s_answer;
}
cout << '\n';
}
void printResult(const string &reason, int answer) {
cout << '\n';
cout << "Game over:" << reason << '\n';
cout << "Number of times correct: " <<< answer / 2 << '\n';
cout << "-------------------" << '\n';
}
© 2024 OneMinuteCode. All rights reserved.