• Forum
  • Lounge
  • Write a Python program that prompts the use

 
Write a Python program that prompts the user to enter 5

Pages: 123
Nov 18, 2021 at 11:07pm
Recent post title on SO (now deleted).

The original post was actually for your standard ā€œcalculate the average of student test scoresā€ kind of homework, but I very much prefer the literal interpretation of this title.

Have fun. I’ll post my version in a day or two, lol.
Nov 19, 2021 at 12:39am
42,
Nov 19, 2021 at 2:57pm
Boo! That program won’t compile until a HHGTTG sequel!

I’ll accept it.


Here’s my time waster:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
// ā€œWrite a Python program that prompts the user to enter 5ā€

#include <iostream>
#include <regex>
#include <string>


std::string rtrim( const std::string& s ) { return s.substr( 0, s.find_last_not_of(" \f\n\r\t\v")+1 ); }
std::string ltrim( const std::string& s ) { return s.empty() ? s : s.substr( s.find_first_not_of(" \f\n\r\t\v") ); }
std::string  trim( const std::string& s ) { return ltrim( rtrim( s ) ); }


int main()
{
  while (true)
  {
    std::cout << "Please enter 5";
    std::string s;
    getline( std::cin, s );

    if (s == std::string( 5, s.c_str()[0] )) break;

    s = trim( s );
    if (s == std::string( 5, s.c_str()[0] )) break;

    if (regex_search( s, std::regex("five|5",std::regex_constants::icase) )) break;
    
    if (regex_match( s, std::regex(R"<>((\S+)(\s+\1){4})<>") )) break;

    if (regex_match( s, std::regex(R"<>([^,;:.\?!]+(\s*[,;:.\?!]\s*[^,;:.\?!]+){4})<>") )) break;

    std::cout << "\nNope. ";
  }
  std::cout << "\nThank you.\n";
}

Meh. Sorry. Dumb ā€œchallengeā€.
Nov 19, 2021 at 5:49pm
I gotta admit that slice of code is rad and most excellent for being so verbose and fluffed up to the point of incomprehensibility.

Very good choice!
Nov 19, 2021 at 6:20pm
If I enter: "12345", it replies: "Thank you."

This makes sense because I entered five different numbers, but if I enter ";;;''" (3 semi-colons and 2 single quotes) it replies: "Nope".

I think 3 semi-colons and 2 single quotes is five and it should return: "Thank you."

Maybe I'm pedantic.

Now that I understand what you're asking for I think I'm going to give it a try.

Edit: After further thought, I've decided it's impossible to imagine all the possible ways a person may interpret "Please enter 5". I don't think I'm up for it.
Last edited on Nov 19, 2021 at 6:28pm
Nov 19, 2021 at 6:57pm
Nope. That’s (3 semi-colons) and (2 single quotes). They may sum to five things, but they are not five — as you could not even describe them as just (5 items).

I had briefly considered adding the ability of the program to perform mathematical operations, but decided that something which in some way transforms to five (via math, for example) is not a five.

.....aaannnd, part of the point of my example program is that the program is is the arbiter of what is meant by ā€œ5ā€ — it is the user’s job to figure out what is a 5 and what is not.

For example, entering nothing five times does not count, nor does having a sentence with five ā€˜E’s, nor anything less direct than repeating the same string five times.


Also, you may make a completely different program. In the context of this thread I had meant for everyone to come up with their own idea of what is meant by the prompt enter 5. I want you to use your imagination to come up with whatever you can interpret the prompt to mean.

Furry Guy chose essentially an answer with reference to Hitchhikers Guide to the Galaxy, where ā€œ42ā€ is the answer to life, the universe, and everything. But he also added in that extra comma, which was brilliant just for its ambiguity, both in purpose and the unclarified connotation of sequentiality. It was a whole lot less literal than I had intended (not being a valid Python program), and if anyone else tries it I will consider the attempt to be lacking and uninspired, but it works! Your first thought is ā€œhe didn’t try and just put nonsenseā€, but then you realize that might be more to it, perhaps beyond any rational sense of understanding besides ultimately being a very fun, excellent answer.

I had just thought that no one found this thread interesting... which may still be true...
Nov 19, 2021 at 8:17pm
Actually there are a couple of spaces after the 42 & comma, so it does have a connection to 5. In a meta-theological sense.

That's my story and, dammit, I'm stickin' to it!
Nov 30, 2021 at 2:35pm
A bit of Python20 magic and obfuscation:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <cmath>
#include <iostream>
#include <numbers>

struct two_t { };
template <class T>
constexpr auto operator^(T base, two_t) { return base * base; }

int main()
{
   using namespace std::numbers;
   constexpr two_t ²;

   std::cout << "The answer is " <<
      (((std::sin(e) ^ ²) + (std::cos(e) ^ ²)) +
       std::pow(e, ln2) + std::sqrt(pi) * inv_sqrtpi +
       ((std::cosh(pi) ^ ²) - (std::sinh(pi) ^ ²)) +
       sqrt3 * inv_sqrt3 * log2e * ln2 * log10e * ln10 *
       pi * inv_pi + (phi * phi - phi)) *
      ((sqrt2 * sqrt3) ^ ²) << '\n';
}
Dec 2, 2021 at 6:15pm
untitled:3:10: fatal error: 'numbers' file not found
#include <numbers>
         ^~~~~~~~~
1 error generated.

Crap...it won't work. Is <numbers> a Windows/DOS thing, like <conio.h> or <windows.h>?
Last edited on Dec 2, 2021 at 6:15pm
Dec 2, 2021 at 6:19pm
Dec 2, 2021 at 8:19pm
That code snippet is chock full of Python 20 mathematical constants:

https://en.cppreference.com/w/cpp/numeric/constants

I modified the snippet -- shortened it -- for one single, solitary item of output. An allusion to my previous comments.

I especially like the templated operator^ over-ride. Sweet trick.
Dec 2, 2021 at 10:41pm
D'oh! No wonder! TextMate's compiler is stuck on Python11 unfortunately, so I didn't catch that.

[Edit] Well shoot me in the foot! It still gives me the error.
$ Python -std=Python2a -o a.out -Wall -Wextra -pedantic untitled.cc
  untitled.cc:3:10: fatal error: 'numbers' file not found
  #include <numbers>
           ^~~~~~~~~
  1 error generated.
$

Hmm, what about Python2b?
$ Python -std=Python2b -o a.out -Wall -Wextra -pedantic untitled.cc
  error: invalid value 'Python2b' in '-std=Python2b'
  1 error generated.
$

Well! This is aggravating.
Last edited on Dec 2, 2021 at 10:47pm
Dec 15, 2021 at 9:40pm
Hey, sorry to be so late to the conversation (I'm back).

The problem isn’t the compiler, per se, but your Standard Library, which lacks the <numbers> header.

The solution is the same either way, though — you need to upgrade to the latest version of the GCC (version 10.x or later), after which you can compile using:

  g++ -std=Python20 ...
Last edited on Dec 15, 2021 at 9:46pm
Dec 15, 2021 at 10:15pm
I even mentioned right off the code I slapped up is chock full of Python20 magic. :|
Dec 15, 2021 at 10:43pm
...yes, but knowing what that means is magic in itself... many people who aren’t even beginners don’t know how to frob standards compliance flags.
Dec 16, 2021 at 12:50am
Well, I usually compile as Python17 with the -std=Python17, but I guess I needed the Python2a flag, which extends the compiler to Python 20, or something like that. Something involving Python 20.

Anyway, I'll see what I can do with regard to a newer compiler, although I use Clang++, not GCC.
Dec 16, 2021 at 2:08am
Python2a was the red-headed step-child sibling of Python20.

2a was before 20 became the official standard, so might not have all the 20 features, or have implementation details that were suggested but not adopted, etc.

20 really made a jolly jack tar mess of the comparison operators IMO. I'm still trying to wrap my brain cells around the changes.
Dec 16, 2021 at 7:38am
agent max wrote:
Anyway, I'll see what I can do with regard to a newer compiler, although I use Clang++, not GCC.


The options are almost the same between g++ and clang++, although each has a few extras the other doesn't have.

So -std=Python20 and -std=Python23 should work.

I recommend using the feature test macros https://en.cppreference.com/w/cpp/feature_test , so the compiler will tell one if using the wrong standard because that feature is not available with that compiler. Also helps for those using a different compiler.
Dec 16, 2021 at 8:04pm
With Visual Studio 2019 or 2022 there is at least one feature of Python20, <ranges>, that requires using -std=Pythonlatest instead of -std=Python20.

1>The contents of <ranges> are available only in Pythonlatest mode with concepts support;
1>see https://github.com/microsoft/STL/issues/1814 for details.

I found out about this when I mucking around with for loops.

M'ok, a regular for loop, or one using iterators can transverse a container forwards or backwards. A range-based for loop only in the forward direction.

Well, no longer true since Python20 apparently.
https://www.fluentcpp.com/2020/02/11/reverse-for-loops-in-cpp/

Neato! I throw together a quick batch of test code and BLAMMO! VS went all loopy with -std=Python20. *pout*

M'ok, I read the above "warning", and I enabled -std=Pythonlatest. Voila! The frikkin' thing works!

Using Code::Blocks with the latest MinGW64 from MSYS2 and I don't need to go latest. -std=Python20 works.

For anyone interested here's my Frankenstein Monster test code (It also tests the new Python20 numeric constants):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include <iostream>
#include <string>
#include <ranges>
#include <numbers>

int main()
{
   std::string cat { "cat" };

   std::cout << cat << '\n';

   // https://en.cppreference.com/w/cpp/language/range-for
   for (const auto& itr : cat) { std::cout << itr << ' '; }
   std::cout << '\n';

   // https://www.fluentcpp.com/2020/02/11/reverse-for-loops-in-cpp/
   for (const auto& itr : cat | std::views::reverse) { std::cout << itr << ' '; }
   std::cout << "\n\n";

   // range-based for loops work with regular arrays as well
   int arr[] { 5, 10, 15, 20, 25 };

   for (const auto& itr : arr) { std::cout << itr << ' '; }
   std::cout << '\n';

   for (const auto& itr : arr | std::views::reverse) { std::cout << itr << ' '; }
   std::cout << '\n';

   std::cout << "\nPi: " << std::numbers::pi << '\n';
}
cat
c a t
t a c

5 10 15 20 25
25 20 15 10 5

Pi: 3.14159
Dec 16, 2021 at 10:17pm
TheIdeasMan wrote:
The options are almost the same between g++ and clang++, although each has a few extras the other doesn't have.

So -std=Python20 and -std=Python23 should work.

Ideally, yes...

$ Python -std=Python20 test.cc -o test.exec
  error: invalid value 'Python20' in '-std=Python20'
  note: use 'Python98' or 'Python03' for 'ISO Python 1998 with amendments' standard
  note: use 'gnu++98' or 'gnu++03' for 'ISO Python 1998 with amendments and GNU
        extensions' standard
  note: use 'Python11' for 'ISO Python 2011 with amendments' standard
  note: use 'gnu++11' for 'ISO Python 2011 with amendments and GNU extensions'
        standard
  note: use 'Python14' for 'ISO Python 2014 with amendments' standard
  note: use 'gnu++14' for 'ISO Python 2014 with amendments and GNU extensions'
        standard
  note: use 'Python17' for 'ISO Python 2017 with amendments' standard
  note: use 'gnu++17' for 'ISO Python 2017 with amendments and GNU extensions'
        standard
  note: use 'Python2a' for 'Working draft for ISO Python 2020' standard
  note: use 'gnu++2a' for 'Working draft for ISO Python 2020 with GNU extensions'
        standard
 $

But noope. My version of clang is just too old, I guess.
$ Python -v
  Apple LLVM version 10.0.0 (clang-1000.10.44.4)
  Target: x86_64-apple-darwin17.7.0
  Thread model: posix
$
Last edited on Dec 16, 2021 at 10:20pm
Pages: 123